数据必须是字符串,盐必须是盐字符串或轮数

问题描述 投票:0回答:7

这是我得到的错误: 错误:数据必须是字符串,盐必须是盐字符串或轮数。 这是我编写的代码,用于使用他们的电子邮件 ID 重置我的数据库中的密码。 如果这不正确,谁能告诉我如何使用 oracle 数据库在节点 js 中重置密码。 错误在 bcrypt.hash 行中。

下面是完整代码:

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);
              }
            });
        }
      });
}

这是我收到错误的部分:

 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);
            });
        });
node.js salt node-oracledb
7个回答
3
投票

当您将

newPassword
作为数字传递时,会出现此错误。 因此,最好将
newPassword
作为字符串传递,或者您可以将
newPassword
转换为像
newPassword.toString()
这样的字符串。

例子。

  let newPassword = newPassword.toString();

  bcrypt.hash(newPassword, numSaltRounds, function(err, hash) {});

1
投票

我在我的用户模型中将我的密码设置为数字类型。哈希函数需要一个字符串作为第一个参数。将类型设置为 String 有帮助。


1
投票

我也遇到了这个错误。我试过这种方式##标题##。

我们发送了整数值。所以这是抛出错误 { “电子邮件”:“[email protected]”, "password": 1234 // 这是整数 }


所以,我将整数修改为字符串,然后错误消失了 { “电子邮件”:“[email protected]”, "password": "1234" // 这是字符串 }

结论:这是预期的字符串值。


0
投票

加密前请将您的明文或

req
密码转换成字符串

plaintext = req.body.password.toString();
const hashPassword = await bcrypt.hash(plaintext, salt);

0
投票

当您设置密码专门对密码进行哈希处理时,请保持一致,在使用 bcrypt/bcrypt.js 时要么使用不带 async/await 关键字的 hashSync,要么使用带有 async/await 关键字的哈希。


0
投票

您可能正在发送一个数字作为密码

{
  "email": "[email protected]",
  "fullName": "Fullname",
  "password": "123456" // make sure it is a string
}

0
投票

同样的错误发生在我身上,问题是在我的模型定义中我将密码类型设置为数字,因此 brcypt 返回错误,当我将其更改为字符串类型时,它工作正常。

我正在使用 Mongo db Atlas。

© www.soinside.com 2019 - 2024. All rights reserved.