如何俱乐部从不同的承诺中做出回应,并将其作为单个JSON对象归还给客户



我试图根据不同条件在MongoDB集合中获取文档数量的数量。

我能够获取数据并形成一个单个JSON对象,但我无法将其发送给客户端。

在以下代码中,您可以看到它是我的服务器文件(server.js)我要在哪里进行get api调用,我想在其中进行多个数据库查询并返回承诺中的值。

rfid是猫鼬模型,sub是我初始化的空json对象。我正在尝试将数据数到一个json对象中,但是我无法将JSON对象(sub)作为对此get api调用的响应。

这可能是范围的问题,但我找不到它。

请帮助我。

server.js

app.get('/dashboard', function(req,res){
     var  sub = {};
     var sub1,sub2,sub3,sub4,sub5,sub6,sub7 = {};

    // no of cars entered
    Rfid.find( {entryTime :{$exists :true }})
    .then(function(res,err){
        if(err) console.log(err);
        var length = res.length;
         sub1 = {
             "no_of_cars_entered" : length
        }
         sub  = Object.assign(sub,sub1);
    });
    // no of cars waiting for inspection
    Rfid.find( {entryTime :{$exists :true },inwardInspecStartTime:{$exists:false}})
    .then(function(res,err){
        if(err) console.log(err);
        var length = res.length;
         sub2 = {
            "no_of_cars_waiting_for_inspection" : length
       }
        sub  = Object.assign(sub,sub2);

    });

    //no of cars inspected
    Rfid.find( {inwardInspecEndTime :{$exists :true }})
    .then(function(res,err){
        if(err) console.log(err);
        var length = res.length;
         sub3 = {
            "no_of_cars_inspected" : length
       }
       sub  = Object.assign(sub,sub3);
     });
    //no of cars waiting for invoice (inward REJECTION)
    Rfid.find( {inwardInspecEndTime :{$exists :true },invoiceTime:{$exists:false}})
    .then(function(res,err){
        if(err) console.log(err);
        var length = res.length;
         sub4 = {
            "no_of_cars_waiting_for_invoice" : length
       }
       sub  = Object.assign(sub,sub4);
    });
   // no of cars invoiced
    Rfid.find( {invoiceTime :{$exists :true }})
    .then(function(res,err){
        if(err) console.log(err);
        var length = res.length;
          sub5  = {
            "no_of_cars_invoiced" : length
       }
       sub  = Object.assign(sub,sub5);
    });
    //no of cars waiting for delivery ibnl
    Rfid.find( {invoiceTime :{$exists :true },deliveryInspecStartTime:{$exists :false}})
    .then(function(res,err){
        if(err) console.log(err);
        var length = res.length;
        sub6= {
            "no_of_cars_waiting_for_delivery" : length
       }
       sub  = Object.assign(sub,sub6);       
    });

    // no of cars delivered
    Rfid.find( {deliveryInspecEndTime :{$exists :true }})
    .then(function(res,err){
        if(err) console.log(err);
        var length = res.length;
          sub7 = {
            "no_of_cars_delivered" : length
       }
       sub  = Object.assign(sub,sub7);
       console.log(sub);

    })
    console.log(sub);

});

结果我得到了输出我得到:

{ no_of_cars_entered: 6,
  no_of_cars_waiting_for_inspection: 1,
  no_of_cars_inspected: 5,
  no_of_cars_invoiced: 4,
  no_of_cars_waiting_for_invoice: 1,
  no_of_cars_waiting_for_delivery: 0,
  no_of_cars_delivered: 4 }
{}

我必须得到的

预期输出:

{ no_of_cars_entered: 6,
  no_of_cars_waiting_for_inspection: 1,
  no_of_cars_inspected: 5,
  no_of_cars_invoiced: 4,
  no_of_cars_waiting_for_invoice: 1,
  no_of_cars_waiting_for_delivery: 0,
  no_of_cars_delivered: 4 }
{ no_of_cars_entered: 6,
  no_of_cars_waiting_for_inspection: 1,
  no_of_cars_inspected: 5,
  no_of_cars_invoiced: 4,
  no_of_cars_waiting_for_invoice: 1,
  no_of_cars_waiting_for_delivery: 0,
  no_of_cars_delivered: 4 }

我假设您在尝试发送响应之前不等待任何诺言返回(您的摘要实际上并没有显示您发送回复时),但是在做出响应时多个请求,您需要等待所有请求,然后再发送响应,然后再返回。这是使用ES6本机Promise lib。

的示例

app.get('/dashboard', function(req,res){
   let promiseArr = [
      Rfid.find( {entryTime :{$exists :true }}),
      Rfid.find( {entryTime :{$exists :true },inwardInspecStartTime:{$exists:false}}),
      Rfid.find( {inwardInspecEndTime :{$exists :true }}),
      Rfid.find( {inwardInspecEndTime :{$exists :true },invoiceTime:{$exists:false}}),
      Rfid.find( {invoiceTime :{$exists :true }}),
      Rfid.find( {invoiceTime :{$exists :true },deliveryInspecStartTime:{$exists :false}}),
      Rfid.find( {deliveryInspecEndTime :{$exists :true }})
   ]
   
   Promise.all(promiseArr)
    .then((resp) => {
      //build your obj here
      // resp[0] = the result of promiseArr[0] etc
      let obj = {};
      //populate obj
      res.send(obj);
      
    }).catch((err) => {
      res.status(500).send(err);
    })
}

最新更新