【问题标题】:findQ in mongooseQ在猫鼬中找到
【发布时间】:2014-08-16 09:12:17
【问题描述】:

想给变量x赋值找到结果,怎么办。谢谢。

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

【问题讨论】:

    标签: node.js mongodb mongoose promise


    【解决方案1】:

    你不能把它放在var x*,这就是 JavaScript 并发的工作原理。请参阅 my answer here 以更广泛地了解该主题。

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

    你不必嵌套,因为 promises 链

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

    * 除非您愿意尝试实验性功能,例如尚未处于节点稳定状态的生成器。

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 2019-03-28
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 2021-04-07
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多