2016-02-25 104 views
0

我见过其他人有同样的问题,看着他们的解决方案,但我不知道他们是否帮助。我似乎无法用在线笔记和简单的日志语句来调试我的代码。我对Express和护照很陌生,所以我希望能提供任何帮助或有用的链接。passportjs TypeError:undefined不是函数

这里是代码,依赖关系和错误。

错误:passportjs类型错误:未定义是不是一个函数 - >>返回完成(NULL,用户)是错误的

依赖行:

"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", 

文件名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

http://stackoverflow.com/help/mcve –

+0

感谢指南使用本网站@JasonS刚开始 – ghosting999

回答

1

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

function(req, email, password, done) 
+0

太感谢你了,它适用于现在。我之前尝试过,它给了我一个问题,说res没有在下面定义。为了解决这个问题,我一定弄错了一些东西。 res.render( '商业/注册',{ 错误: '您必须填写所有的区域。', FNAME:FNAME, 公司名称:公司名称, 电子邮件:电子邮件,用户名 :用户名,密码 :密码 }); – ghosting999

+0

您不应该从护照处理程序发回回应。相反,请使用Passport提供的重定向和Flash消息功能。 – robertklep

+0

我会再次感谢@robertklep – ghosting999

相关问题