node.js - 将两个响应数据合并为一 - NodeJS

标签 node.js mongoose

我有一个 NodeJS 服务器,它从两个不同的 API 获取数据,然后我想将这两个 API 的结果合并到一个 JSON 响应中。我向您发送代码:

EventModal.eventSearch(eventReq, type, async function (eventRes) {
       EventModal.getBoostEvents(eventReq, type, async function (eventBoostRes) {
                           res.json({
                                status: true,
                                data: eventRes,
                                eventBoostRes: eventBoostRes,
                            });
        });
  });

我希望 eventReseventBoostRes 位于 data 的一个响应中。那么如何才能实现这一目标?

eventReseventBoostRes为查询结果。

提前致谢。

最佳答案

你可以像这样组合它们:

EventModal.eventSearch(eventReq, type, async function (eventRes) {
    EventModal.getBoostEvents(eventReq, type, async function (eventBoostRes) {
        res.json({
            status: true,
            data: { 
                eventRes, 
                eventBoostRes
            }
        });
    });
});

关于node.js - 将两个响应数据合并为一 - NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47868222/

相关文章:

javascript - 如何解决 Mongoose Connect UNKNOWN 问题?

node.js - Node-RED 中的多个 HTTP 请求

javascript - 如何在JS中使用带有Promise结果的postMessage

javascript - 使用 Node.js 驱动程序打印和更新 MongoDB 中的所有文档

node.js - 如何解决 Mongoose 的排序错误?

javascript - MongoDB - 多个查询和单个文档结果

node.js - 无法重新启动 redis-server.service : Unit redis-server. 找不到服务

node.js - 如何使用 github 部署 firebase 函数

node.js - mongoDB + Mongoose : choosing the right schema for hierarchical data

javascript - 未处理的PromiseRejection警告: undefined in Mongoose