mohamed adel
mohamed adel

Reputation: 715

Nodejs filter data and send it as response

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?

Upvotes: 0

Views: 5159

Answers (3)

linthertoss
linthertoss

Reputation: 164

Try with:

 getRequest(timeProducts)
            .then(response => {
                const result = response.data.result
                response.json(result.filter(data => {
                    return data.pointsPrice 
                }));
            })
            .catch(errorMessage => {
                res.json(errorMessage)
            });

Upvotes: -1

SuleymanSah
SuleymanSah

Reputation: 17868

You 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.

Upvotes: 4

Travis James
Travis James

Reputation: 1939

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)
            });

Upvotes: 2

Related Questions