I am creating a sign up page and getting all the information from user using form but everything is working fine except the part where I am checking if the user exists or not
router.post("/test", async(req, res) => {
let username = req.body.user;
await con.query(checkUser, , (err, result) => {
if (err) throw err;
else if (result.length > 0) {
console.log(result)
res.send("User found")
} else {
console.log(result)
res.send("User not found")
console.log(username)
}
})
})
checkUser = "SELECT * FROM `users` WHERE `username` = (?)";
I created separate post router to check the bug and the problem is my first else part is not working every time I get result as an empty array weather the user exists or not and res user not found, also when I console the username I got it as undefined
I test the sql separately in the admin page of mysql and its working perfectly am getting result
2
Answers
There could be a few reasons why you’re experiencing issues with your sign-up page:
body-parser
middleware to parse the request body. It is necessary for extracting form data.username
field in your form is correctly named and matchesreq.body.user
. Ensure there are no typos or mismatches.Dont use callback here to get your result instead do something like
let result = await conn.query(…);
And wrap the whole code in try catch to catch the errors.