0

Trying to save user into mySql with bcrypt. But return Error: data and salt arguments required.

Here is my code

auth controller:

export const register = (req, res) => {
//CHECK EXISTING USER
const q = "SELECT * FROM user WHERE email = ? OR username = ?";

db.query(q, [req.body.email, req.body.username], (err, data) => {
    if (err) return res.status(500).json(err);
    if (data.length) return res.status(409).json("User already exists!");

    //Hash the password and create a user
    const salt = bcrypt.genSaltSync(10);
    const hash = bcrypt.hashSync(req.body.password, salt);

    const q = "INSERT INTO users(`username`,`email`,`password`) VALUES (?)";
    const values = [req.body.username, req.body.email, hash];

    db.query(q, [values], function (err, data) {
        if (err) return res.status(500).json(err);
        return res.status(200).json("User has been created.");
    });
  });

};

As you can see, I already define salt, and hash by

const salt = bcrypt.genSaltSync(10); const hash = bcrypt.hashSync(req.body.password, salt);

So I have no idea why return Error: data and salt arguments required.

Here is the error returned:

Error: data and salt arguments required at Object.hashSync (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/bcrypt/bcrypt.js:91:15) at Query. (file:///Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/controllers/auth.js:14:27) at Query. (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/mysql/lib/Connection.js:526:10) at Query._callback (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/mysql/lib/Connection.js:488:16) at Query.Sequence.end (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24) at Query._handleFinalResultPacket (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/mysql/lib/protocol/sequences/Query.js:149:8) at Query.EofPacket (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/mysql/lib/protocol/sequences/Query.js:133:8) at Protocol._parsePacket (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/mysql/lib/protocol/Protocol.js:291:23) at Parser._parsePacket (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/mysql/lib/protocol/Parser.js:433:10) at Parser.write (/Users/rickyyeung/Desktop/nodejsProject/rick20230510booking/api/node_modules/mysql/lib/protocol/Parser.js:43:10) rickyyeung@Rickys-MacBook-Air api %

  • 3 `?` on `VALUES` instead of 1 - [ref](https://stackoverflow.com/questions/70238296/trying-to-pass-variables-into-a-prepared-statement-in-javascript-node-js)? – danblack May 18 '23 at 06:21

1 Answers1

0

I see two problems here,

one : You passed only one value to the sql, but needed to pass 3 values :

const q = "INSERT INTO users(`username`,`email`,`password`) VALUES (?)";
const values = [req.body.username, req.body.email, hash];

When inserting value into the DB, in this case you provided only one value VALUES (?)";, when it should be like VALUES (?,?,?)"; to match arguments you passed in the const values : req.body.username, req.body.email, hash

as for the hash issue :

It seems like the req.body.password is empty. make sure the check for null or undefined.

Shachar297
  • 599
  • 2
  • 7