2016-04-13 17 views
0

你好,所以我在我的passport.js下面的代碼:不能在passport.js傳遞的參數 - MEAN堆棧

passport.use('local-signup', new LocalStrategy({ 
    // by default, local strategy uses username and password, we will override with email 
    usernameField : 'email', 
    passwordField : 'password', 
    nameField: 'fullname', 
    passReqToCallback : true // allows us to pass back the entire request to the callback 
}, 
function(req, email, password, done, fullname) { 

    // asynchronous 
    // User.findOne wont fire unless data is sent back 
    process.nextTick(function() { 

    // 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 
    User.findOne({ 'local.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) { 
      return done(null, false, req.flash('signupMessage', 'That email is already taken.')); 
     } else { 

      // if there is no user with that email 
      // create the user 
      var newUser   = new User(); 

      // set the user's local credentials 
      newUser.local.email = email; 
      newUser.local.password = password; 
      newUser.local.fullname = fullname; 
      newUser.local.role = "default"; 

      // save the user 
      newUser.save(function(err) { 
       if (err) { 
        throw err; 
       } 
       console.log(newUser); 
       return done(null, newUser); 

      }); 
     } 

    });  

    }); 

})); 

,我需要保存全稱到數據庫但遺憾的是不添加因爲是完成後的最後一個參數。但是如果在完成之前生病了,那麼返回的結果是沒有找到,並給我一個應用程序崩潰。

您認爲什麼是解決方案?

+0

你不能只從'req'參數中得到全名(nameField)嗎? – lipp

+0

不,我不能,我只是試過。 – NinetyHH

回答

0

您可以從req參數中檢索完整名稱。如果您使用的是bodyparser,那麼它就像req.body.fullname一樣簡單。在需要的地方使用。您需要確保您使用電子郵件和密碼從表單發送fullname

護照的本地策略不支持除usernameFieldpasswordField之外的其他輸入。如果您需要來自用戶的其他輸入,則需要從原始請求中檢索它們。