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

155
Views
Error when post data to serve express using hash bcrypt to hash password

I try to write a Api using express and sequelize. I'm writing create user function and use bcrypt to hash password.

 const createNewUser = (data) => {
  return new Promise(async (resolve, reject) => {
    try {
      let check = await checkUserEmail(data.email)
      if (check === true) {
        resolve({
          errcode: 1,
          errMessage: 'Your email is already exist!',
        })
      }
      let hashPassWordFromBcrypt = await hashUserPassWord(data.passWord)
  

      await db.User.create({
        email: data.email,
        passWord: hashPassWordFromBcrypt,
        firstName: data.firstName,
        lastName: data.lastName,
        address: data.address,
        phoneNumber: data.phoneNumber,
        gender: data.gender === '1' ? true : false,
        roleId: data.roleId,
      })
     resolve({
       errcode: 0,
       errMessage: 'OK',
     })
} catch (error) {
  reject(error)
}

}) }

const salt = bcrypt.genSaltSync(10)

const hashUserPassWord = (password) => {
  return new Promise(async (resolve, reject) => {
    try {
      let hashPassWord = await bcrypt.hashSync(password, salt)
      resolve(hashPassWord)
    } catch (error) {
      reject(error)
    }
  })
}

const checkUserEmail = (email) => {
  return new Promise(async (resolve, reject) => {
    try {
      const user = await db.User.findOne({ where: { email } })
      if (user) {
        resolve(true)
      }
      resolve(false)
    } catch (e) {
      reject(e)
    }
  })
}

I use postman to test. The problem is when I send data with a email already exist.The response: errCode: 1 and errMasege: Your email is already exist but a new user created with same email. But if I post a request with a already exist email and without passWord, no new User created. Help me. Thanks.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

A resolve or reject statement does not exit the promise executor function. You should write

if (check === true) {
  return resolve({
    errcode: 1,
    errMessage: 'Your email is already exist!',
  })
}

otherwise the db.User.create operation will be carried out afterwards.

(Your code

if (user) {
  resolve(true)
}
resolve(false)

also seems to assume that resolve exits the executor function. But in this particular case, it does not matter, because resolve(false) has no effect if the promise has already resolved to true.)

about 4 years ago · Santiago Gelvez 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!