简体   繁体   English

在Mongoose查询中使用JavaScript Promise

[英]Using javascript promises with Mongoose queries

Normally if I was going to run multiple mongoose queries I would use the built in promise to chain them all together. 通常,如果我要运行多个猫鼬查询,我将使用内置的Promise将它们链接在一起。 In this case, the user chooses which schemas to search. 在这种情况下,用户选择要搜索的架构。 This could be one of them or both. 这可以是其中之一,也可以是两者兼而有之。 The following example uses the post data to define which schemas to search and if one is false, it should continue through the promise chain. 以下示例使用发布数据来定义要搜索的模式,如果一个模式为假,则应在promise链中继续进行。 Right now the final promise is being called before the queries. 现在,在查询之前会调用最终的承诺。

Example in my express controller: 我的快递控制器中的示例:

app.post('/custom-search', function (req, res) {
     var single = false
     var multi = false


      if(req.body.single){
          var single = true
      }

      if(req.body.multi){
          var multi = true
      }

    var promise = new Promise(function (resolve, reject) {
        if(multi){
            multiSchema.find({}, function (err, result) {
                if(!err){
                    console.log(result);
                    resolve()
                }
            })
        }else{
            resolve()
        }
    }).then(function (value) {
        if(single){
            singleSchema.find({}, function (err, result) {
                if(!err){
                    console.log(result);
                    resolve()
                }
            })
        }else{
            resolve()
        }
    }).then(function (value) {
        console.log("done");
    })
})

}); });

output: 输出:

>done
>[singleResults]
>[multiResults]

done should be printing last so that is the first problem. 完成应该是最后打印,这是第一个问题。

Like we discussed, few things had to be clean up. 正如我们所讨论的,几乎不需要清理任何事情。 First by actually using and returning the promise for it work properly, Second, creating a mini-promise within your first .then() to resolve and reject your single conditional statement. 首先,通过实际使用并返回保证其正常工作的诺言,其次,在您的第一个.then()创建一个微型承诺来解决和拒绝您的单个条件语句。 And third, handling/catching promises. 第三,处理/捕捉承诺。

I wrote a pseudo version of your code to illustrate my point of view, hopefully it may be of a good use. 我编写了您的代码的伪版本来说明我的观点,希望它可能会有用。

app.get('/custom-search', function (req, res) {
        // Manipulating values to test responses
        var single = false;
        var multi = true;

        var promise = new Promise(function (resolve, reject) {
            if (multi) {
                setTimeout(function () {
                    resolve('MULTI RESOLVED!');
                }, 3000);
            } else {
                reject('MULTI REJECTED!');
            }
        })
            .then(function (value) {
                new Promise(function (resolve, reject) {
                    if (single) {
                        setTimeout(function () {
                            resolve('SINGLE RESOLVED!');
                        }, 3000);
                    } else {
                        reject('SINGLE REJECTED!');
                    }
                })
                    .catch(function (error) {
                        console.log('SINGLE ERROR!', error);
                    })
                    .then(function (value) {
                        console.log('SINGLE DONE', value);
                    });
            })
            .catch(function (error) {
                console.log('MULTI ERROR!', error);
            });
        return promise;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM