2014-10-29 133 views
0

控制台语句从不打印。我不知道为什么。我正在写一个函数来列出所有字符(一个用户有很多字符)。该功能尚未完整编写。它只是返回用户指定的电子邮件。Mongo /回调函数和节点问题 - 从不调用回调

由于某种原因,但它永远不会返回。它输出“This statement prints”但从来没有“为什么不这样打印”声明!

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('[email protected]', function(err, okay){ 
    console.log("Why doesn't this ever print?"); 
}); 

回答

0

我需要调用回调!该死的!

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); 
}); 

我希望这可以帮助您了解如何编写回调。