大佬教程收集整理的这篇文章主要介绍了node.js – 如何处理异步.护照和猫鼬的findOrCreate方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
var UserscheR_833_11845@a = new scheR_833_11845@a({ firstname: @R_696_10495@ng,lastname: @R_696_10495@ng,email: @R_696_10495@ng,accounts: [] });
accounts数组包含代表facebook帐户的对象,例如{provider:“facebook”,uid:“someFacebookId”}.
我的身份验证策略如下所示:
// Authentication Strategy passport.use(new FacebookStrategy({ clientID: CONfig.fb.appId,clientSecret: CONfig.fb.appSecret,callBACkURL: CONfig.fb.callBACkURL },function(accessToken,refreshToken,profile,donE) { // asynchronous verification,for effect... process.nextTick(function () { User.find({ 'accountS.Uid': profile.id,'accounts.provider': 'facebook' },function(err,olduser) { if(olduser._id) { console.log('User: ' + olduser.firstname + ' ' + olduser.lastname + ' found and logged in!'); done(null,olduser); } else { var newuser = new User(); var account = {provider: "facebook",uid: profile.iD}; newuser.accounts.push(account); newuser.firstname = profile.name.givenName; newuser.lastname = profile.name.familyName; newuser.email = "TBD..."; newuser.save(function(err) { if(err) { throw err; } console.log('New user: ' + newuser.firstname + ' ' + newuser.lastname + ' created and logged in!'); done(null,newuser); }); } }); }); } ));
问题:查询我的数据库(User.find(…))后,立即执行回调函数,而不等待我的数据库回答.这会导致未定义的olduser对象.因此,每次该用户尝试登录时,我都会将同一用户的同一用户添加到我的数据库中.
如何正确处理此异步回调?
以上是大佬教程为你收集整理的node.js – 如何处理异步.护照和猫鼬的findOrCreate方法全部内容,希望文章能够帮你解决node.js – 如何处理异步.护照和猫鼬的findOrCreate方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。