skip to Main Content

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


  1. There could be a few reasons why you’re experiencing issues with your sign-up page:

    1. Make sure you have set up the body-parser middleware to parse the request body. It is necessary for extracting form data.
    2. Double-check that the username field in your form is correctly named and matches req.body.user. Ensure there are no typos or mismatches.
    3. Verify that your database connection is correctly established. Check the connection configuration to ensure it is properly set up.
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search