节点|通过对象和GET匹配req.param.id递归循环

时间:2019-03-04 04:25:39

标签: javascript node.js rest ecmascript-6

在Node.js中进行以下练习

// SEND response 
router.get('/:weatherId', (req, res, next) => {
    const id = req.params.weatherId

    const request = [
      {
      "timestamp":"2015-09-01T16:00:00.000Z",
      "temperature": 27.1,
      "dewPoint": 16.7,
      "precipitation": 0
    },
    {
        "timestamp":"2016-09-01T16:00:00.000Z",
        "temperature": 27.1,
        "dewPoint": 16.7,
        "precipitation": 0
      },
      {
        "timestamp":"2015-09-01T16:00:00.000Z",
        "temperature": 27.1,
        "dewPoint": 16.7,
        "precipitation": 0
      },
      {
          "timestamp":"2014-09-01T16:00:00.000Z",
          "temperature": 27.1,
          "dewPoint": 16.7,
          "precipitation": 0
        },

    ]

    if (id === /* exact timestamp */) {
      res.send(request) /* request should send only the object that matches the timestamp */
    } else {
      res.status(404).json({
        message: 'Ouch Weather Not found'
      })
    }
  })

因此,在尝试进行GET调用时,即localhost:5000/weather/2015-09-01T16:00:00.000Z

递归遍历对象数组,检索仅发送与res.send()中传递的时间戳完全匹配的对象的req.param.id的最佳方法是什么?

0 个答案:

没有答案