最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何将来自不同承诺的响应联系起来,并将其作为单个json对象返回给客户端

IT培训 admin 4浏览 0评论

如何将来自不同承诺的响应联系起来,并将其作为单个json对象返回给客户端

我试图根据不同的条件得到一些mongodb集合中的文档数量。

我能够获取数据并形成一个json对象,但我无法发送它以响应客户端。

在下面的代码中,您可以看到它是我的服务器文件(server.js),我正在进行GET api调用,我正在尝试执行多个数据库查询并在promises中返回值。

Rfid是mongoose模型,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);
    })
}

如何将来自不同承诺的响应联系起来,并将其作为单个json对象返回给客户端

我试图根据不同的条件得到一些mongodb集合中的文档数量。

我能够获取数据并形成一个json对象,但我无法发送它以响应客户端。

在下面的代码中,您可以看到它是我的服务器文件(server.js),我正在进行GET api调用,我正在尝试执行多个数据库查询并在promises中返回值。

Rfid是mongoose模型,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);
    })
}
发布评论

评论列表 (0)

  1. 暂无评论