【问题标题】:Pass multiple Promises into Node JS render将多个 Promise 传递给 Node JS 渲染
【发布时间】:2017-10-16 22:41:09
【问题描述】:

我想将来自 3 个不同 Promise 的数据传递到 nodejs 中的渲染函数中,以便与 pug 一起使用。

var promise = require('promise');

var statusChart = new promise(function (resolve, reject) {
            a.aggregate(
            [
                {
                    $group: {
                        _id: '$status',
                        count: {$sum: 1}
                    }
                }
            ], function (err, status) {
                if (err) {
                    console.log(err);
                    reject(err);
                } else {
                    resolve(status);
                }
            });
        });

        var severityChart = new promise(function (resolve, reject) {
            a.aggregate(
            [
                {
                    $group: {
                        _id: '$severity',
                        count: {$sum: 1}
                    }
                }
            ], function (err, vuln) {

                if (err) {
                    console.log(err);
                    reject(err);
                } else {
                    resolve(vuln);
                }
            });
        })

        var countChart = new promise(function (resolve, reject) {
            a.count(function (err, count) {
                if (err) {
                    console.log(err);
                    reject(err);
                } else {
                    resolve(count);
                }
            });
        })

        statusChart.then((message) => {
            console.log(message);
        });

        severityChart.then((data) => {
            console.log(data);
        });

        countChart.then((item) => {
            console.log(item);
        });

上面的代码工作正常,会返回我的结果

[ { _id: 'Medium', count: 6 },
  { _id: 'High', count: 15 },
  { _id: 'Low', count: 1 } ]
[ { _id: 'New', count: 1 },
  { _id: 'Closed', count: 1 },
  { _id: 'In Progress', count: 11 },
  { _id: 'Pending', count: 9 } ]
22

问题:如何在渲染函数中传递这些数据。

res.render('graphs', {info: statusChart, vuln: severityChart, count: countChart});

当我以这种方式尝试时,我在哈巴狗方面得到以下结果

var 结果 = {"_75":1,"_83":0,"_18":null,"_38":{"onRejected":null,"promise":{"_75":0,"_83" :0,"_18":null,"_38":null}}}; var status = {"_75":1,"_83":0,"_18":null,"_38":{"onRejected":null,"promise":{"_75":0,"_83":0, "_18":null,"_38":null}}}; var total = {"_75":1,"_83":0,"_18":null,"_38":{"onRejected":null,"promise":{"_75":0,"_83":0, "_18":null,"_38":null}}};

【问题讨论】:

  • 使用Promise.all
  • 你真的应该编写一个辅助函数,它接受一个 id,调用 new Promisea.aggrate,并返回结果的承诺。

标签: javascript node.js pug


【解决方案1】:

您正在向 info、vuln 和 count 变量传递 Promise。那时还没有解决。要使其正常工作,请执行以下操作

....
return Promise.all([statusChart, severityChart, countChart])
       .then(([statusChartVal,severityChartVal,countChartVal]) => {
         return res.render('graphs', {info: statusChartVal, vuln: 
                severityChartVal, count: countChartVal});
       });
....

【讨论】:

  • Promise.all 在仅解决所有承诺时解决。如果任何一个 Promise 失败,就会调用 catch。所以,添加 catch 块。
【解决方案2】:

新的承诺((解决,拒绝)=> { // 做点什么 //

      return new Promise((resolve, reject) => {
                            //Do Something //
                         Resolve(true); 
                        });
                 Promise.all(Promise).then((responses) => {
                        resolve({
                            status: true,
                            data: data   
                        });
                 });                    
    }).then((response) => {
        res.json(response);
    });

【讨论】:

    猜你喜欢
    • 2017-12-03
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多