小编fai*_*n98的帖子

UnhandledPromiseRejectionWarning:API 未处理的承诺拒绝

我正在尝试 console.log 天气 API 的一些数据,但是当我查找位置时收到错误消息

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

到目前为止,我的代码在我的服务器上是这样的

app.post('/weather', (req, res) => {
    const url = `https://api.darksky.net/forecast/${DARKSKY_API_KEY}/${req.body.latitude},${req.body.longitude}?units=auto`
    axios({
      url: url,
      responseType: 'json'
    }).then(data => res.json(data.data.currently)).catch(error => { throw error})
  })

app.listen(3000, () => {
    console.log("Server has started")
})
Run Code Online (Sandbox Code Playgroud)

和我的 JavaScript

  fetch('/weather', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': …
Run Code Online (Sandbox Code Playgroud)

javascript json node.js es6-promise

0
推荐指数
1
解决办法
2021
查看次数

标签 统计

es6-promise ×1

javascript ×1

json ×1

node.js ×1