2016-02-25 99 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

相關問題