I am new in node with koa and postgresql. I have a created a user login api but i'm getting 404 not found error. My queries and checks are working as i checked on console but ctx.body not working. How i can handle multiple responses with koa ctx.body? Don't know why no ctx.body is working. How we can solve this issue? Hope you understand my issue.
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",
}
}
});
}
});