'res.send return two or more items from a map method
I'm doing a get request and it fetches properly. From there I have to return that data based on the if statement, but the data needs to be returned as an object.
In the object, I need to have two or more data but it does not return. If I keep it only one, it works fine.
My question is how can I return two or more items in res.send
with the data coming from the map function?
fetch(`API KEY`, {
method: 'GET',
})
.then(response => response.json())
.then(myData => {
if (myData[0].data === undefined) {
res.send(myData.map(data => {data.id, data}))
} else if (myData[0].data !== undefined) {
res.send(myData.map(data => {data.id, data.data}))
}
});
Solution 1:[1]
If you want to return an Object implictly from your map function, it must be wrapped with ()
, like
myData.map(data => ({data.id, data}))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Heiko Theißen |