代码之家  ›  专栏  ›  技术社区  ›  Rylore

将多个承诺传递到Node JS render

  •  0
  • Rylore  · 技术社区  · 7 年前

    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 total={“_75”:1,“_83”:0,“_18”:null,“_38”:{“onRejected”:null,“promise”:{“_75”:0,“_83”:0,“_18”:null,“_38”:null}}}};

    2 回复  |  直到 7 年前
        1
  •  1
  •   user-developer    7 年前

    您正在将承诺传递给info、vuln和count变量。这些问题在那时还没有解决。要使其工作,请执行以下操作

    ....
    return Promise.all([statusChart, severityChart, countChart])
           .then(([statusChartVal,severityChartVal,countChartVal]) => {
             return res.render('graphs', {info: statusChartVal, vuln: 
                    severityChartVal, count: countChartVal});
           });
    ....
    
        2
  •  0
  •   Damini Suthar    7 年前

    //做点什么//

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