我正在尝试使用koa和postgresql在节点中创建post RestApi。我有一个唯一的电子邮件验证,并且想检查电子邮件是否存在,它应该显示该邮件已经存在的消息用户。我的查询正在工作,但是ctx.body无法工作。该api每次都会创建新用户。当数据库中存在电子邮件时,应显示msg“该电子邮件已存在用户”。为什么我的ctx.body无法正常工作?希望你能理解。
router.post('/createNewUser',async (ctx) => {
var name = ctx.request.body.name;
var email = ctx.request.body.email;
var password = ctx.request.body.password;
if (
!name ||
!email ||
!password
) {
ctx.response.status = 400;
ctx.body = {
status: 'error',message: 'Please fill all the fields'
}
} else {
await ctx.app.pool.query("SELECT * FROM users WHERE email = $1",[`${email}`],(err,result) => {
if(result.rows.length > 0){
ctx.body = {
exceptions: "",status: 200,error: false,message: "user already exists with this email",};
}
});
await ctx.app.pool.query(`INSERT INTO USERS (name,email,password) VALUES ('${name}','${email}','${bcrypt.hashSync(password,10)}')`);
const {rows} = await ctx.app.pool.query("SELECT * FROM users ORDER BY id DESC LIMIT 1");
ctx.body = {
exceptions: "",message: "User registered Successfully",data: rows[0],};
}
});