2017-10-16 94 views
0

我想將來自3個不同promises的數據傳遞到nodej中的render函數中,以便與pug一起使用。將多個Promise傳入Node JS呈現

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

問:我如何通過在渲染功能,這個數據。 ('圖表',{info:statusChart,vuln:severityChart,count:countChart});

當我嘗試這樣我得到哈巴狗側

VAR的結果= { 「_75」 的結果如下:1, 「_ 83」:0, 「_ 18」:空, 「_ 38」:{ 「onRejected」:空, 「承諾」:{ 「_ 75」:0, 「_ 83」:0, 「_ 18」:空, 「_ 38」:空}}}; var status = {「_75」:1,「_ 83」:0,「_ 18」:null,「_ 38」:{「onRejected」:null,「promise」:{「_75」:0,「_ 83」:0 , 「_ 18」:空, 「_ 38」:空}}}; var total = {「_75」:1,「_ 83」:0,「_ 18」:null,「_ 38」:{「onRejected」:null,「promise」:{「_75」:0,「_ 83」:0 , 「_ 18」:空, 「_ 38」:空}}};

+0

使用'Promise.all'。 – Bergi

+0

你真的應該編寫一個帶有id的助手函數,調用'new Promise'和'a.aggrate',並返回結果的承諾。 – Bergi

回答

0

您將承諾傳遞給info,vuln和count變量。那些問題沒有解決。爲了得到這個工作,請執行下列操作

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

Promise.all解析何時只有所有的promise都解決了。所以,添加catch塊。 –

0

新的承諾((解析,拒絕)=> {// 做些什麼//

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