在node.js和postgres中为每个项添加响应以进行循环

时间:2018-06-11 06:44:35

标签: node.js postgresql express promise

我有一系列像这样的学校:

{
 "schools": [
            {
                "name": "S1",
                "id": 1
            },
            {
                "name": "S2",
                "id": 2
            },
            {
                "name": "S3",
                "id": 3
            }
        ]
}

每个学校都有时间表。为了得到它,我在一个承诺中迭代学校数组,当我得到响应时,我得到一个像这样的数组

{
 "schools": [
            {
                "name": "S1",
                "id": 1
            },
            {
                "name": "S2",
                "id": 2
            },
            {
                "name": "S3",
                "id": 3
            }
        ],
"schedules": [
            [],
            [
                {
                    "id_schedule": 58,
                    "hour1": "13:00:00",
                    "hour2": "20:00:00",
                    "id_schools_schedule": 2
                }
            ],
            [
                {
                    "id_schedule": 59,
                    "hour1": "06:30:00",
                    "hour2": "22:30:00",
                    "id_schools_schedule": 3
                }
            ]
        ]
}

我想知道。如何确定每个项目的响应? 这是我的代码

for (var i =0; i < datosRes.schools.length; i++){
                array_horarios.push(ObtSchedule(datosRes.schools, i))
              }
              Promise.all(array_horarios).then(response => {
                datosRes.horarios = response;
                eq.local = data;
              }).catch(err => {
                return res.json(200,{"datos":datosRes});
              })

function ObtHorario(schools, i){
  return new Promise(function(resolve, reject){
    var id_school = schools[i].id;
    Mod_Schedule.obtSchedule(id_school,function(error, data){
      if(error || data.error){
        errorDB = {"error_log": error, "error_data": data.error};
        reject(errorDB)
      }else{
        resolve(data)
      }
    })
  })
}

我做错了什么? 我得到了回复,但只是我想在每个学校项目中添加时间表

提前致谢

1 个答案:

答案 0 :(得分:0)

第一件事: 您可以向一个请求发送一个响应。因此,发送多个回复的问题无效。

在这里你可以做什么,你可以按照他们的日程安排获得一系列学校。

如果您使用的是MongoDB,请执行以下操作:

使用Aggregate查询:

db.schools.aggregate([
    {
        $match: {} // Your condition to match schools here
    },
    {
        $lookup: {
                      from:"schedules",
                      localField: id,
                      foreignField: id_schools_schedule,
                      as: "schedulesData"
                 }
    },
]);

在这里,您将获得如下数据:

[
        {
            "name": "S1",
            "id": 1,
    "schedulesData": []
        },
        {
            "name": "S2",
            "id": 2,
    "schedulesData": [{
                "id_schedule": 58,
                "hour1": "13:00:00",
                "hour2": "20:00:00",
                "id_schools_schedule": 2
            }]
        },
        {
            "name": "S3",
            "id": 3,
    "schedulesData": [
            {
                "id_schedule": 59,
                "hour1": "06:30:00",
                "hour2": "22:30:00",
                "id_schools_schedule": 3
            }
        ]
        }
    ]