【发布时间】:2020-04-11 23:18:00
【问题描述】:
我是 koa 和 postgresql 的新节点。我创建了一个用户登录 api,但出现 404 not found 错误。我的查询和检查工作正常,因为我在控制台上检查但 ctx.body 不工作。我如何使用 koa ctx.body 处理多个响应?不知道为什么没有 ctx.body 工作。我们如何解决这个问题? 希望你能理解我的问题。
router.post('/userLogin', async (ctx) => {
var email = ctx.request.body.email;
var password = ctx.request.body.password;
if (
!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}`],
async (err, result) => {
if(err){
console.log(err);
throw err;
}
if (result) {
await bcrypt.compare(password, result.rows[0].password).then(function (res) {
if (res === true) {
ctx.body = {
status: 200,
message: "User login successfully",
data: result.rows[0],
};
}else{
ctx.body = {
status: 400,
message: "Incorrect password",
}
}
});
}else{
ctx.body = {
status: 400,
message: "Invalid email",
}
}
});
}
});
【问题讨论】:
标签: javascript node.js postgresql koa ctx