使用javascript承诺与Mongoose查询

时间:2022-04-24 01:23:09

Normally if I was going to run multiple mongoose queries I would use the built in promise to chain them all together. 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. Right now the final promise is being called before the queries.

通常,如果我要运行多个mongoose查询,我会使用内置的promise将它们链接在一起。在这种情况下,用户选择要搜索的模式。这可能是其中之一或两者兼而有之。以下示例使用post数据来定义要搜索的模式,如果一个模式为false,则应继续通过promise链。现在,在查询之前调用最终的承诺。

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.

完成应该打印最后,这是第一个问题。

1 个解决方案

#1


0  

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. And third, handling/catching promises.

就像我们讨论的那样,很少有东西需要清理。首先实际使用并返回承诺,使其正常工作,其次,在您的第一个.then()中创建一个小承诺来解决并拒绝您的单个条件语句。第三,处理/捕捉承诺。

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

#1


0  

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. And third, handling/catching promises.

就像我们讨论的那样,很少有东西需要清理。首先实际使用并返回承诺,使其正常工作,其次,在您的第一个.then()中创建一个小承诺来解决并拒绝您的单个条件语句。第三,处理/捕捉承诺。

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