3

This is the error I am getting: Error: data must be a string and salt must either be a salt string or a number of rounds. This is the code that i have written is for resetting the password in my db using their email id. If this is not correct can anyone tell me how to reset the password in node js using oracle database. The error is in the bcrypt.hash line.

Below is the whole code:

function changePassword(email, newPassword, callback) {

    var oracledb = require('oracledb');
    oracledb.outFormat = oracledb.OBJECT;

    oracledb.getConnection({
        user          : '',
        password      : '',
        connectString : ''
      },
      function(err, connection) {
        if (err) {
          return callback(new Error(err));
        }
        bcrypt.hash(newPassword, numSaltRounds, function(err, hash) {
          if (err) { return callback(err); }
          connection.execute(
            ' select password as "password" = : hash, ' +
            ' from jsao_users ' +
            ' where email = :email ', [hash, email], { autoCommit: true },
            function(err, result) {
              if (err) {
                console.log(err);
                doRelease(connection);
                return callback(new Error(err));
              }
              doRelease(connection);
              callback(null, result.rowsAffected > 0);
            });
        });

        // Note: connections should always be released when not needed
        function doRelease(connection) {
          connection.close(
            function(err) {
              if (err) {
                console.error(err.message);
              }
            });
        }
      });
}

And this is the part where I am getting the error:

 bcrypt.hash(newPassword, numSaltRounds, function(err, hash) { //this is the line
          if (err) { return callback(err); }
          connection.execute(
            ' select password as "password" = : hash, ' +
            ' from jsao_users ' +
            ' where email = :email ', [hash, email], { autoCommit: true },
            function(err, result) {
              if (err) {
                console.log(err);
                doRelease(connection);
                return callback(new Error(err));
              }
              doRelease(connection);
              callback(null, result.rowsAffected > 0);
            });
        });
MT0
  • 143,790
  • 11
  • 59
  • 117
Anurag
  • 95
  • 4
  • 17

7 Answers7

3

This error is getting while you pass the newPassword as number. So, better to pass the newPassword as string or you can convert the newPassword to string like newPassword.toString().

Example.

  let newPassword = newPassword.toString();

  bcrypt.hash(newPassword, numSaltRounds, function(err, hash) {});
Pruthvish
  • 56
  • 6
2

I'm also getting this error. i tried this way## Heading ##.

we have send the values in integer. So this is throw error { "email": "vignesh@gmail.com", "password": 1234 // this is integer }


So, I've modified integer to String then error was gone { "email": "vignesh@gmail.com", "password": "1234" // this is String }

Conclusion: this is expect the String values.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 26 '22 at 07:49
1

I had set my password as Number type in my user model. Hash function expects a string as first argument. Setting the type to String helped.

1

You are probably sending a number for password

{
  "email": "yourEmail@mail.com",
  "fullName": "Fullname",
  "password": "123456" // make sure it is a string
}
Ismoil Shokirov
  • 2,213
  • 2
  • 16
  • 32
0

Please make your plaintext or req password into string before encripting.

plaintext = req.body.password.toString();
const hashPassword = await bcrypt.hash(plaintext, salt);
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

When you are setting the password specifically hashing the password, be consistent, when using bcrypt/bcrypt.js either use the hashSync without the async/await key word or use the hash with the async/await keyword.

0

The same error was happening to me, the problem is that in the definition of my model I put the type of my password as Number, for this reason brcypt returned the error, when i changed it to type String, it worked normally.

I was using Mongo db Atlas.

Wes
  • 1