0

I have this function which saves user in database:

exports.saveUser = ({ first_name, last_name, email, password }) => {
    const query = "insert into users (first_name, last_name, email, password_hash) values ($1, $2, $3, $4) RETURNING *";
    bcrypt.hash(password, 10, async function (err, hash) {
        const res = await db.query(query, [first_name, last_name, email, hash]);
        return res.rows[0];
    });
    return res;
};

I have async function inside bcrypt callback where insertion is happening and where I'm returning inserted row. How do i make it to return for saveUser function?

So at the end saveUser() will be that res.rows[0] record?

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
Gia Nebieridze
  • 141
  • 3
  • 14

1 Answers1

1
exports.saveUser = async ({ first_name, last_name, email, password }) => {
    const query = "insert into users (first_name, last_name, email, password_hash) values ($1, $2, $3, $4) RETURNING *";
    let hash = await bcrypt.hash(password, 10);
    const res = await db.query(query, [first_name, last_name, email, hash]);
    return res.rows[0];
};

i have made bcrypt.hash await instead of callback. then made saveUser async ad it worked

Gia Nebieridze
  • 141
  • 3
  • 14