Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How do I fix this async function?

I am finishing writing a register-controller for my web-app, however my async function seems to not work. As soon as I send the form, the page returns {"message: email already exists"}. I have tried naming the async function, naming module.exports, it just won't work .In the console I get this:

Express server is running on port no : 3000
App has started succesfully
Connection to DB succeded.
check user imported
checkUserAsync () => {
console.log('check user imported')
return {

async function(email) {
  console.log('check user used')
  let query = `SELECT count(email) as count FROM users where email = "${email}"`

  return new Promise((resolve, reject) => {
    mysqlConnection.query(query, function(error, results) {
      if (error) {
        return reject(error)
      }
      console.log('Promise results: ',results)
      return resolve(results[0].count)
    })
  })
}
}
}
User check if already exist:  { function: [AsyncFunction: function] }

This is my register-controller:

const mysqlConnection = require ('../config')
const bcrypt = require ('bcrypt')
const saltRounds = 10;
const checkUserAsync = require('../utils/checkUser')


module.exports.register = async function(req,res) {
  var today = new Date();
  var users={
      firstname: req.body.firstname,
      lastname: req.body.lastname,
      email: req.body.email,
      password: req.body.password,
      signup_date: today,
      last_login_date: today
  }

  let userCheck

  try {
    // check if the user mail exists
    userCheck = await checkUserAsync(users.email)
    console.log('checkUserAsync', checkUserAsync)

    // hash the user password
    users.password = await bcrypt.hash(users.password, saltRounds)
  } catch(e) {
    return res.status(500).json({message: 'Internal Server Error'})
  }
  console.log('User check if already exist: ', userCheck)
  if (userCheck) {
    return res.status(400).json({
      message: 'email already exists'
    })
  }

  let insertsUser

  try {
    insertsUser = await insertUser(users)

    users.password = await bcrypt.hash(users.password, saltRounds)
  } catch (e) {
    return res.status(500).json({message: 'Internal Server Error'})
  }
  if (insertsUser) {
    res.redirect('/')
  }

  // insert the user in the database
  users.id = await insertUser(users)
  console.log('[register] user', users)

  res.redirect('/');

/**
 * Insert a new user in the database
 * 
 * @param {Object} users User data object
 */
async function insertUser(users) {

  return new Promise((resolve,reject)=> {
    mysqlConnection.query('INSERT INTO users SET ?',users, function (error, results) {
      if (error) {
        return reject(error)
      }
      return resolve(results)
    })
  })
}}

And this is checkUser.js:

const mysqlConnection = require ('../config')

/**
* Search a user by email and return the count from the db
* 
* @param {string} email the user email
*/
module.exports = () => {
  console.log('check user imported')
  return {

    async function(email) {
      console.log('check user used')
      let query = `SELECT count(email) as count FROM users where email = "${email}"`

      return new Promise((resolve, reject) => {
        mysqlConnection.query(query, function(error, results) {
          if (error) {
            return reject(error)
          }
          console.log('Promise results: ',results)
          return resolve(results[0].count)
        })
      })
    }
  }
}

Comments