Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Login Page, want to check for existing users

For my registration page, I want to check for existing users before adding a new user to avoid duplicate usernames. For some reason, after I check if an existing username exists in the database, it does not run any of the code afterwards.

app.post("/register", (req, res) => {
  const username = req.body.username;
  const password = req.body.password;
  var newuser = false;

  db.query("SELECT * FROM users WHERE username = ?;", [username], (err, result) => {
      
    if (err) {
      res.send({err: err})
    }

    if (result.length == 0) {
      newuser = true;
      console.log(newuser + "one")
    }
  })

    if (newuser) {
    console.log(newuser + "two")
    bcrypt.hash(password, saltRounds, (err, hash) => {
      if (err)
      console.log(err)
    
      db.query(
        "INSERT INTO users (username, password) VALUES (?,?)",
        [username, hash],
        (err, result) => {
        if (err)
          console.log(err);
          }
      )
    })
  }
})

In this example, the only output I get is "trueone" ... meaning that it evaluated result.length to 0 and set newuser = true.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is because you are not waiting to the result of the first select. You can use async function or move the if(new user) condition into the select callback.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!