Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么这一承诺会提前兑现?_Node.js_Express_Mongoose - Fatal编程技术网

Node.js 为什么这一承诺会提前兑现?

Node.js 为什么这一承诺会提前兑现?,node.js,express,mongoose,Node.js,Express,Mongoose,Product.find()的承诺在更新最高价格的循环完成之前得到解决。只有当数据操作完成时,我如何才能解决承诺 Product.find({'prodType': req.params.type}).lean().exec(function (err, product) { if (err) { res.status(400); } for (var i = 0; i < product.length; i++) { (fun

Product.find()的承诺在更新最高价格的循环完成之前得到解决。只有当数据操作完成时,我如何才能解决承诺

Product.find({'prodType': req.params.type}).lean().exec(function (err, product) {

    if (err) {
        res.status(400);
    }

    for (var i = 0; i < product.length; i++) {

        (function(u) {

            Option.find({'prodId': product[i].productId}).lean().sort({price: -1}).limit(1).exec(function (err, highest) {

                product[u].price = highest;
                // not updated in returned object

            });

        })(i);

    }

}).then(function (product) {

    res.send(product);

});
Product.find({'prodType':req.params.type}).lean().exec(函数(err,Product){
如果(错误){
物质状态(400);
}
对于(变量i=0;i
将所有代码从
.exec()
回调移动到
。然后
,并返回一个新的承诺,该承诺在所有其他承诺完成时解析

Product.find({'prodType': req.params.type}).lean().exec()
.then(function (product) {
    var promises = [];
    for (var i = 0; i < product.length; i++) {

        (function(u) {

            promises.push(Option.find({'prodId': product[i].productId}).lean().sort({price: -1}).limit(1).exec(function (err, highest) {

                product[u].price = highest;
                // not updated in returned object

            }));

        })(i);

    }
    return Promise.all(promises).then(function () {
        // we want to pass the original product array to the next .then
        return product; 
    });

}).then(function (product) {

    res.send(product);

}).catch(function (err) { // the power of promise error catching!
    // If any error occurs in any of the db requests or in the code, this will be called.
    res.status(400);
});
Product.find({'prodType': req.params.type}).lean().exec()
.then(function (product) {
    var promises = product.map(function (p) {
        return Option.find({'prodId': p.productId}).lean().sort({price: -1}).limit(1).exec(function (err, highest) {

            p.price = highest;

        }));
    });
    return Promise.all(promises).then(function () {
        // we want to pass the original product array to the next .then
        return product; 
    });

}).then(function (product) {

    res.send(product);

}).catch(function (err) { // the power of promise error catching!
    // If any error occurs in any of the db requests or in the code, this will be called.
    res.status(400);
});