Node.js Mongo/回调和节点故障-从不调用回调
console语句从不打印。我不知道为什么。我正在编写一个函数来列出所有字符(一个用户有许多字符)。函数尚未完全编写。它只是将指定的电子邮件返回给用户 因为某种原因,尽管它永远不会回来。它输出“This statement prints”,但从不输出“This why not print”语句Node.js Mongo/回调和节点故障-从不调用回调,node.js,callback,mongoose,Node.js,Callback,Mongoose,console语句从不打印。我不知道为什么。我正在编写一个函数来列出所有字符(一个用户有许多字符)。函数尚未完全编写。它只是将指定的电子邮件返回给用户 因为某种原因,尽管它永远不会回来。它输出“This statement prints”,但从不输出“This why not print”语句 UserSchema.methods.usersCharacters = function(email,cb){ User.findOne( {'local.email' : email }).exe
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?");
});
我需要回电话!该死 您需要调用回调,请参见以下内容:
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);
});
我希望这有助于您理解如何编写回调