2

I have a NodeJS server that takes data from two different API's, and then I want to combine the result from both in one JSON response. Here I am sending you the code:

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

I want eventRes and eventBoostRes in one response in data.So how can I achieve that ?

eventRes and eventBoostRes are query result.

Thanks in advance.

Damini Suthar
  • 1,470
  • 2
  • 14
  • 43
  • Could you expand on what `eventRes` and `eventBoostRes` contain. – Brahma Dev Dec 18 '17 at 12:13
  • Possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – Brahma Dev Dec 18 '17 at 12:14
  • In JS everything is as simple as `obj.key=val` where val can be a js assignable entity like a integer, object or even function. It csn be also `obj[key]=val` where key can be a variable – Nidhin David Dec 18 '17 at 12:33
  • `res.json({ status: true, data: {eventRes:eventRes, eventBoostRes: eventBoostRes} })` – Nidhin David Dec 18 '17 at 12:35

2 Answers2

2

You can combine them like this:

EventModal.eventSearch(eventReq, type, async function (eventRes) {
    EventModal.getBoostEvents(eventReq, type, async function (eventBoostRes) {
        res.json({
            status: true,
            data: { 
                eventRes, 
                eventBoostRes
            }
        });
    });
});
maxpaj
  • 6,029
  • 5
  • 35
  • 56
  • it contains in different array object. So I need to make modification in response. – Damini Suthar Dec 18 '17 at 12:30
  • 1
    Can you paste what `eventRes` and `eventBoostRes` could look like? And paste the result you want `data` to contain in the response? – maxpaj Dec 18 '17 at 12:32
1

Question not very clear.

However, it sounds like you are getting 2 arrays and you want to return a single array in the response. Quick ( and dirty ) way to do this is use array.concat( anotherArray ) function:

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

However, this will cause 2 queries to run in sync and is not optimal. You could optimise this to use promises and run 2 queries in parallel:

Promise.all([ // this will run in parallel
  EventModal.eventSearch(eventReq, type),
  EventModal.getBoostEvents( eventReq, type )
]).then( function onSuccess([ eventRes, eventBoostRes ]) {
  res.json({
    status: true,
    data: eventRes.concat( eventBoostRes )
  });
});

On the other hand; this should probably be handled at the query level.

Zilvinas
  • 5,518
  • 3
  • 26
  • 26