我有一个简单的nodejs应用程序的代码,它允许用户登录并注册他们的帐户。以下是用于注册过程的代码的一部分:
$scope.sendForm= function(){
var formData= $scope.formModel;
console.log($scope.formModel.length);
for(var i=0; i<formData.length; i++){
var sparseKeys = Object.keys(formData);
console.log(sparseKeys);
}
}
他们使用passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done){
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function(){
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 = newUser.generateHash(password);
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
来延迟process.nextTick()
的执行,评论说:function()
。这篇评论是在讨论User.findOne wont fire unless data is sent back
吗?这个评论谈论的是什么data being sent back to client
?为什么我们必须推迟执行?如果我们没有延迟执行,会有什么影响呢?