Mongo / Callbacks和Node麻烦 - 永远不会调用回调

时间:2014-10-29 08:38:23

标签: node.js callback mongoose

控制台语句永远不会打印。我不知道为什么。我正在编写一个列出所有字符的函数(一个用户有很多字符)。该功能尚未完整编写。它只是返回具有指定电子邮件的用户。

出于某种原因虽然它永远不会回来。它输出"此语句打印"但从来没有"为什么这不打印"声明!

UserSchema.methods.usersCharacters = function(email,cb){
  User.findOne( {'local.email' : email }).exec(function(err, user){
    if (err) console.log("oops");
    console.log("This statement prints");
    return user;
  });
};

UserSchema.methods.usersCharacters('a@gmail.com', function(err, okay){
  console.log("Why doesn't this ever print?");
});

2 个答案:

答案 0 :(得分:0)

我需要拨回电话! DANGIT!

答案 1 :(得分:0)

您需要调用回调,请参阅:

var a = function(input ,callback){
   if ( input == "true" ){
      callback(null ,"it's true");
   } else {
      callback(true ,"it's false")
   }
};

a(true ,function(err ,res){
    console.log(err);
    console.log(res);
});

a(false ,function(err ,res){
    console.log(err);
    console.log(res);
});

我希望这有助于您了解如何编写回调。

相关问题