找到猫鼬

时间:2014-06-25 17:21:10

标签: node.js mongodb mongoose promise

想要分配给变量x值找到结果,该怎么做。谢谢。

var x;
someModel.findQ({"name":"John"}.then(function(result){
    result// here i have my object, how i can make this in var x ?
});

1 个答案:

答案 0 :(得分:0)

您无法将其置于var x *中,这就是JavaScript并发的工作原理。有关此主题的更广泛观点,请参阅my answer here

someModel.findQ({"name":"John"}.then(function(result){
    // use result here
});

你不必嵌套,因为承诺链

someModel.findQ({"name":"John"}.then(function(result){
    // use result here
    return someOtherModel.findQ({"name" : result.name }); 
}).then(function(obj){
    // you can access the return result of someOtherModel here
});

*除非您愿意尝试像节点稳定的生成器这样的实验性功能。