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

1 messages · Page 1 of 1 (latest)

steel pawn
#

Here is my code

auth controller:

//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.
bleak maple
#

Console.log(hash) ?

obsidian nimbus
#

that error comes up if either of the args are missing, even though it makes it sound like it indicates both are missing

#

so the salt is probably fine

#

i reckon req.body.password is undefined and some middleware body parser somewhere has fallen over

#

you gotta validate your inputs!

steel pawn
#

Thank you for your reply

#

can you write a code to show me the proper way to fix it?

obsidian nimbus
#

Not really I don't even know how your form data is being transmitted

#

Handle it the same way you handle any other POST request that you know works right now