1

Currently I have the following statement for return an array from a get route:-

return res.json(dataChunk);  

Now I also have another array results[1] which I also want to return. So how to do it?

2 Answers 2

1

You only get one response from a given request. So, if you want to respond with two separate pieces of data, you need to combine them somehow into a single response and send that. You can either merge their data into one data structure or you can send an object with multiple properties where each property represents one piece of you data.

Without seeing the actual code for your request handler, we can't really know exactly what to suggest, but if you had two separate pieces of data dataChunk and results[1] available at the same time and wanted to send them both, you would just do:

res.send({data: dataChunk, results: results[1]});

This would make the response by an object with two properties data and results and the recipient of the response could examine both properties to get both pieces of the response.

Sign up to request clarification or add additional context in comments.

Comments

1

You want to return multiple things from a response? Just wrap it in an object.

const myArray = [1, 2, 3]
const myObject = { foo: 'bar'}

res.json({ myArray, myObject })

1 Comment

Then on client sde can i access by using myArray? If i want to access myArray object from response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.