简体   繁体   English

Nodejs过滤数据并将其作为响应发送

[英]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?甚至使此功能更好的最佳方法是什么?

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.每个请求只能发送一个res.json ,因此在forEach的第一次迭代之后,您的响应被发回并完成请求。 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:或者,不使用forEach ,而是使用filter并直接在res.json函数中对其进行res.json

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

You send response in the first iteration of forEach.您在 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.最好在 catch 块中发送 500 错误代码。

Try with:尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM