【问题标题】:Error: data and salt arguments required (Async)错误:需要数据和盐参数(异步)
【发布时间】:2018-06-23 08:54:39
【问题描述】:

我的 Schema.pre('save') 有问题,在我的模型“用户”中,无法使用 bcrypt 对我的密码进行哈希处理。

我的 app.js,在 mongodb 中使用 mongoose 进行简单连接

mongoose.connect('mongodb://localhost/gederson', {
  useMongoClient: true,

});

const db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
  console.log('Connected on mongo');
});

app.listen(process.env.PORT || 3000, () => {
  console.log('listening');
});

index(app, db);
post(app, db);
admin(app, db);


module.exports = app;

我的路由,我在我的应用程序中创建了创建用户的路由,但是在预“保存”中无法获取哈希密码的“this”。

const Users = db.model('Users');

const newUser = {
          username: req.body.username,
          email: req.body.email,
          password: req.body.password,
        };
        Users.create(newUser, (err) => {
          if (err) throw err;
          res.status = 201;
          return res.send('User created');
        });

我的模型用户,代码预“保存”

const bcrypt = require('bcrypt');

UserSchema.pre('save', (next) => {
  const user = this;
  bcrypt.hash(user.password, 10, (err, hash) => {
    if (err) {
      return next(err);
    }
    user.password = hash;
    return next();
  });
});

const Users = mongoose.model('Users', UserSchema);

module.exports = Users;

堆栈错误:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: data and salt arguments required
    at /home/gedersonchiquesi/ProjetosDev/wos/node_modules/bcrypt/bcrypt.js:114:16
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...

【问题讨论】:

    标签: node.js mongoose bcrypt


    【解决方案1】:

    我今天遇到了类似的问题。

    我通过删除 ES6 语法(箭头函数)解决了这个问题。

    UserSchema.pre('save', function(next) {
      const user = this;
      bcrypt.hash(user.password, 10, function(err, hash) {
         if (err) {
         return next(err);
         }
         user.password = hash;
         next();
      })
    });
    

    【讨论】:

    • 是的,在同一天我解决了这个 pre('save'),箭头函数不接受 'this'
    【解决方案2】:

    因为您在该函数上传递输入的空白​​值

    如果测试邮递员或需要数据输入

    {
        "name":"test",
        "email":"test@test.com",
        "password":"123456"
    }
    

    添加查看我的数据存储代码示例

    
    router.post("/register-user", (req, res, next) => {
        bcrypt.hash(req.body.password, 10).then((hash) => {
            const user = new userSchema({
                name: req.body.name,
                email: req.body.email,
                password: hash
            });
            user.save().then((response) => {
                res.status(201).json({
                    message: "User successfully created!",
                    result: response
                });
            }).catch(error => {
                res.status(500).json({
                    error: error
                });
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多