TypeError:done不是函数?

时间:2018-02-11 17:41:58

标签: node.js express passport-local

app.post('/sign', passport.authenticate('signup', {

    successRedirect: 'login',
    failureRedirect: '/',
    failureFlash: true

}));

passport.use('signup', new LocalStrategy({

    usernameField: "Email",
    passwordField: "Password",
    passReqToCallback: true,

}, function(req, Email, Password, Name, Username, Confirm, done) {

    Name     = req.body.Name;
    Username = req.body.Username;
    Confirm  = req.body.Confirm;

    process.nextTick(function() {

        crud.findOne({ Email: Email }, function(err, user) {

            if (err) {
                return done(err);
            }

            if (user) {
                return done(null, false, console.log("Email is already used"));
            }

            var data = new crud();

            data.Confirm  = Confirm;
            data.Password = Password;
            data.Email    = Email;
            data.Username = Username;
            data.Name     = Name;

            data.save(function(err) {
                if (err) throw err;
                return done(null, data, console.log("Successfullt"));
            })
        })
    })
}));

1 个答案:

答案 0 :(得分:2)

您向passport.use()提供的回调参数太多。

}, function(req, Email, Password, Name, Username, Confirm, done) {

它们的数量和顺序很重要,因为护照策略只会提供某些参数来分配它们的值。而且,参数与JavaScript中的标识符/名称不匹配。

使用本地护照,并且passReqToCallback设置为truethere should be 4 parameters

passport.use(..., function (req, username, password, done) {
    console.log(req      === arguments[0]); // true
    console.log(username === arguments[1]); // true
    console.log(password === arguments[2]); // true
    console.log(done     === arguments[3]); // true

    // ...
});

任何其他变量都应使用函数体内的varlet语句声明:

var Name     = req.body.Name;
var Username = req.body.Username;
var Confirm  = req.body.Confirm;