如何在nodejs中向Json响应添加数据

时间:2017-06-13 05:16:47

标签: javascript json node.js mongodb postgresql

我在项目mongodb和postgresql中使用了2个数据库。我必须将存储在pg中的数据附加到基于用户ID的mongodb返回的Json数据中,所以请在这里帮助我。

预期结果:

{
"previous": [
    {
      "_id": "591453340a59301b9471e3d1",
      "university_id": "7",
      "email": "time@time.com",
      "total_hours": "02:00",
      "universityName":"AAAAA"
    },
     "previous": [
    {
      "_id": "591453340a59301b9471e3d1",
      "university_id": "6",
      "email": "time@time.com",
      "total_hours": "02:00",
      "universityName":"CCCC"
    }
    ]
}

_iduniversity_idemail&安培; total_hours来自mongo,universityName来自PG

来自我的代码 分别考虑mongo json数据和universityName(university_id在两个数据中都很常见)但没有根据university_id

获取如何将它们组合在一起

这是我的代码

entries.find({
        "email": data.email,
    }).toArray().then(function(result) {
        result.map(function(t){
            db.query('select * from universities where _id =$1', [t.university_id],function(err,univers){
                if (err) return callback(new Error('error'));
                result.universityName = univers.rows[0].universityName
                  return callback(null, result);
            })
        })

    }).catch(function(err) {
        return callback(new Error(err));
    })


Mongodb database 

{
      "_id": "591453340a59301b9471e3d1",
      "university_id": "7",
      "email": "time@time.com",
      "total_hours": "02:00" 
    }

Postgresql:

_id |  universityName
1   |  aaaa
6   | bbbb
7   | vvvv

0 个答案:

没有答案