bcrypt-node抛出错误“没有给出callback函数”

我正在使用JWT身份validation创build用户注册。 而我正在使用bcrypt-node模块进行密码哈希和腌制。

useraModel.js

var mongoose =require('mongoose'); var Schema =mongoose.Schema; var bcrypt =require('bcrypt-node'); var UserSchema=new Schema({ name:{type:String, unique:true, required:true}, password:{type:String, required:true} }); UserSchema.pre('save', function (next) { var user = this; if (this.isModified('password') || this.isNew) { bcrypt.genSalt(10, function (err, salt) { if (err) { return next(err); } bcrypt.hash(user.password, salt, function (err, hash) { if (err) { return next(err); } user.password = hash; next(); }); }); } else { return next(); } }); UserSchema.methods.comparePassword = function (passw, cb) { bcrypt.compare(passw, this.password, function (err, isMatch) { if (err) { return cb(err); } cb(null, isMatch); }); }; module.exports = mongoose.model('User', UserSchema); 

passport.js

 var JwtStrategy =require('passport-jwt').Strategy; var User =require('../app/models/user'); var config =require('../config/database'); module.exports=function(passport){ var opts = {}; opts.secretOrKey = config.secret; passport.use(new JwtStrategy(opts, function(jwt_payload, done) { User.findOne({id: jwt_payload.id}, function(err, user) { if (err) { return done(err, false); } if (user) { done(null, user); } else { done(null, false); } }); })); }; 

app.js

 apiRoutes.post('/signup', function(req, res) { if (!req.body.name || !req.body.password) { res.json({success: false, msg: 'Please pass name and password.'}); } else { var newUser = new User({ name: req.body.name, password: req.body.password }); // save the user newUser.save(function(err) { if (err) { return res.json({success: false, msg: 'Username already exists.'}); } res.json({success: true, msg: 'Successful created new user.'}); }); } }); 

当我运行这个代码显示错误 –

 throw "No callback function was given". No Callback function was given 

请帮我解决这个代码。 谢谢

       

网上收集的解决方案 "bcrypt-node抛出错误“没有给出callback函数”"

bcrypt.hash()需要4个参数:

 hash(data, salt, progress, cb) 

这个文档还不清楚:它指出datasaltcb是必需的,这意味着progress不是,但没有它你会得到错误。

如果您不关心跟踪进度,则传递null

 bcrypt.hash(user.password, salt, null, function (err, hash) { ... })