类型错误:做不是一个函数护照
passport.use('local.signup', new Localstrategy({
usernameField: 'email',
passwordField: 'password',
roleField: 'role',
passReqToCallback: true
}, function(req, email, password, role, done) {
req.checkBody('email', 'Invalid Email').notEmpty().isEmail();
req.checkBody('password', 'Invalid Password ').notEmpty().isLength({min:4});
req.checkBody('role', 'Invalid Password').notEmpty();
var errors = req.validationErrors();
if (errors) {
var messages = [];
errors.forEach(function(error) {
messages.push(error.msg);
});
return done(null, false, req.flash('error', messages));
}
// what the...
User.findOne({
'email': email
}, function(err, user) {
if (err) {
return done(err);
}
if (user) {
return done(null, false, {
message: 'Email is already in use.'
});
}
var newUser = new User();
newUser.email = email;
newUser.password = newUser.encryptPassword(password);
newUser.role = role;
newUser.save(function(err, result) {
if (err) {
return done(err);
}
return done(null, newUser);
});
});
}));
我不相信这个设置会做任何事:
function(req, email, password, role, done) {
:
roleField: 'role',
您可以通过这个改变你的函数的参数修复错误
对此:
function(req, email, password, done) {
events.js:163 throw er; //未处理的 '错误' 事件 ^ 类型错误:做不是一个函数 在/Users/ZL/Desktop/nodeJs/debble/passport_config/passport.js:54:20 在/用户/ ZL /桌面/的NodeJS /debble/node_modules/mongoose/lib/model.js:3932:16 –
导致我的用户模式有角色,所以也许角色是必要的,我很困惑,为什么'完成不是一个函数'是关于Promise ?希望你能帮助我,我几乎挣扎了一天 –
@conleywang它与promise没有任何关系。该函数将传递4个参数,第四个参数是'done'回调。通过添加一个'role'参数,你可以将'done'移动到第5个,这将不存在。参数由位置决定,而不是名称。如果你需要额外的信息,比如角色,你需要自己从'req'对象中获取它。 – skirtle
是在字符串部分的那些换行符部分你的原始代码? – Xufox