passportjs TypeError:undefined不是函数

时间:2016-02-25 05:08:40

标签: javascript mongodb express passport.js

我已经看到其他人遇到同样的问题并查看了他们的解决方案,但我不确定他们是否有所帮助。我似乎无法使用在线笔记和简单的日志语句来调试我的代码。我是Express和Passport的新手,所以我会很感激任何帮助或有用的链接。

这是代码,依赖项和错误。

错误:passportjs TypeError:undefined不是函数 - >> return done(null,user)是错误所在的行

依赖关系:

"body-parser": "^1.10.2",
"cookie-parser": "~1.3.3",
"express": "~4.11.1",
"express-session": "^1.10.3",
"hjs": "~0.0.6",
"mongodb": "^1.4.40",
"monk": "*",
"morgan": "^1.5.1",
"passport": "^0.2.1",
"passport-local": "^1.0.0",
"serve-favicon": "^2.2.0",

FILE NAME PASSPORT.JS

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

            // by default, local strategy uses username and password, we will override with email
            usernameField: 'username',
            passwordField: 'password',
            passReqToCallback: true // allows us to pass back the entire request to the callback
        },
        function (req, res, email, password, done) {
            var db = req.db;
            var companyName = req.body.companyName;
            var fname = req.body.fname;
            var email = req.body.email;
            var username = req.body.username;
            //var lname = req.body.lname;
            //var phone = req.body.phone;
            var password = req.body.password;

            // Check if any field has been left blank
            console.log('check if fields filled');
            if (fname === '' || companyName === '' || email === '' || username === '' || password === '' ) {
                console.log('Is this working????');
                res.render('business/register', {
                    error: 'You must fill in all fields.',
                    fname: fname,
                    companyName: companyName,
                    email: email,
                    username: username,
                    password: password
                });
            } else {
                console.log('grab data from database');
                var businesses = db.get('businesses');
                var employees = db.get('employees');
                //TODO: Get visitors too
                //var visitors = db.get('visitors');
                //var staff = db.get('staff');
                //var

                // find a user whose email is the same as the forms email
                // we are checking to see if the user trying to login already exists
                businesses.findOne({'email': email}, function (err, user) {
                    // if there are any errors, return the error

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

                    // check to see if theres already a user with that email
                    if (user) {
                        console.log('user exists');
                        console.log(user);
                        return done(null, false);
                    } else {

                        // if there is no user with that email
                        // create the user
                        console.log('creating user');
                        // set the user's local credentials
                        password = auth.hashPassword(password);

                        // save the user
                        businesses.insert({
                            email: email,
                            password: password,
                            companyName: companyName,
                            //phone: phone,
                            fname: fname,
                            username: username,
                            //lname: lname,
                            logo: '',
                            walkins: false
                        }, function (err, result) {
                            if (err) {
                                throw err;
                            }

                            var businessID = result._id.toString();

                            employees.insert({
                                business: ObjectId(businessID),
                                password: result.password,
                                //phone: result.phone,
                                fname: result.fname,
                                //lname: result.lname,
                                email: result.email,
                                smsNotify: true,
                                emailNotify: true,
                                admin: true
                            },function(err, user){
                                if (err) {
                                    throw err;
                                }
                                console.log(user);
                                //error is right here
                                return done(null, user);
                            });
                        });
                    }
                });
            }

        }
    ));`

1 个答案:

答案 0 :(得分:1)

您的处理程序回调有一个额外的res参数,该参数无效。它的签名应该是:

function(req, email, password, done)