I have an array of data I have a function that filter the array get the data I want and send it as a response.
The problem is the function sends the 1st array only, for example here is the data:
[
{
"name": "2 Hours 30 Min (2.5 Hours)",
"price": 20,
"rewardPoints": 50,
"priceInPoints": 1000,
"minutes": 150,
"id": 8
},
{
"name": "5 Hours",
"price": 40,
"rewardPoints": 100,
"priceInPoints": 1800,
"minutes": 300,
"id": 7
}
]
Code:
getRequest(timeProducts)
.then(response => {
const result = response.data.result;
result.forEach(data => {
if (data.pointsPrice != null) {
res.json({
name: data.name,
price: data.price,
rewardPoints: data.points,
priceInPoints: data.pointsPrice,
minutes: data.minutes
});
}
});
})
.catch(errorMessage => {
res.json(errorMessage);
});
What I get is :
{
"name": "2 Hours 30 Min (2.5 Hours)",
"price": 20,
"rewardPoints": 50,
"priceInPoints": 1000,
"minutes": 150,
"id": 8
}
But I want all.
What am I doing wrong here?
And what's the best approach to even make this function better?
I have an array of data I have a function that filter the array get the data I want and send it as a response.
The problem is the function sends the 1st array only, for example here is the data:
[
{
"name": "2 Hours 30 Min (2.5 Hours)",
"price": 20,
"rewardPoints": 50,
"priceInPoints": 1000,
"minutes": 150,
"id": 8
},
{
"name": "5 Hours",
"price": 40,
"rewardPoints": 100,
"priceInPoints": 1800,
"minutes": 300,
"id": 7
}
]
Code:
getRequest(timeProducts)
.then(response => {
const result = response.data.result;
result.forEach(data => {
if (data.pointsPrice != null) {
res.json({
name: data.name,
price: data.price,
rewardPoints: data.points,
priceInPoints: data.pointsPrice,
minutes: data.minutes
});
}
});
})
.catch(errorMessage => {
res.json(errorMessage);
});
What I get is :
{
"name": "2 Hours 30 Min (2.5 Hours)",
"price": 20,
"rewardPoints": 50,
"priceInPoints": 1000,
"minutes": 150,
"id": 8
}
But I want all.
What am I doing wrong here?
And what's the best approach to even make this function better?
Share Improve this question edited Jan 9, 2020 at 15:18 SuleymanSah 17.9k6 gold badges37 silver badges60 bronze badges asked Jan 9, 2020 at 14:30 mohamed adelmohamed adel 7153 gold badges15 silver badges38 bronze badges3 Answers
Reset to default 4You send response in the first iteration of forEach. Before you send response, you should construct the desired result, and then send.
You can use filter and then map methods of array.
getRequest(timeProducts)
.then(response => {
const result = response.data.result
.filter(data => data.pointsPrice)
.map(({ name, price, points, pointsPrice, minutes }) => ({
name,
price,
rewardPoints: points,
priceInPoints: pointsPrice,
minutes
}));
res.json(result);
})
.catch(errorMessage => {
res.status(500).json(errorMessage);
});
Also better to send 500 error code in the catch block.
You can only send one res.json
per request, so after the first iteration of your forEach
your response is sent back and the request is done. So you need to map your data and save it to a variable, and then send that variable as a response:
getRequest(timeProducts)
.then(response => {
const result = response.data.result
const dataToSend = []
result.forEach(data => {
if (data.pointsPrice != null) {
dataToSend.push({
name: data.name,
price: data.price,
rewardPoints: data.points,
priceInPoints: data.pointsPrice,
minutes: data.minutes,
})
}
});
res.json(dataToSend);
})
.catch(errorMessage => {
res.json(errorMessage)
});
Or, instead of using a forEach
, use a filter
and map over it directly in the res.json
function:
getRequest(timeProducts)
.then(response => {
const result = response.data.result
res.json(result.filter(data => {
if (data.pointsPrice != null) {
return {
name: data.name,
price: data.price,
rewardPoints: data.points,
priceInPoints: data.pointsPrice,
minutes: data.minutes,
}
}
return false
}));
})
.catch(errorMessage => {
res.json(errorMessage)
});
Try with:
getRequest(timeProducts)
.then(response => {
const result = response.data.result
response.json(result.filter(data => {
return data.pointsPrice
}));
})
.catch(errorMessage => {
res.json(errorMessage)
});