【问题标题】:(node:52585) UnhandledPromiseRejectionWarning: Unhandled promise rejection(节点:52585)UnhandledPromiseRejectionWarning:未处理的承诺拒绝
【发布时间】:2021-03-13 22:44:17
【问题描述】:

我有一个函数,我使用导入的 npm 包 current-weather-data 根据位置的经度和纬度获取某个位置的天气。我以前从未使用过 Promise,因为我通常使用 await 和 async,但这似乎很有趣且值得学习……在下面运行这段代码时,它不会呈现页面并给我错误 (node:52585) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag - -unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:52585) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是我的代码:

 app.get('/admin-dashboard', (req, res) => {
    if (req.session.loggedin) {

     
      
        const location = {
            lat: 43.955540,
            lon: -79.480950
        }
         
    

        getWeather(location)
            .then(weather => {
                console.log(`The temperature here is: ${weather.temperature.value}`)
               
         
          
        res.render("admin-dashboard", {
            page_title: "admin-dashboard",
            FN: req.session.firstname,
            LN: req.session.lastname,
            weather: ${weather.temperature.value},
          });
        }).catch(err => console.log(err));
    } else {
        res.render('login.ejs')
    }
})

【问题讨论】:

标签: javascript node.js promise


【解决方案1】:

您没有捕获getWeather 函数引发的错误。您可以在函数中添加.catch,例如

app.get('/admin-dashboard', (req, res) => {
    if (req.session.loggedin) {
      const location = {
            lat: 43.955540,
            lon: -79.480950
        }
      getWeather(location).then(weather => {
        console.log(`The temperature here is: ${weather.temperature.value}`);
        res.render('admin-dashboard', {
          page_title: "admin-dashboard",
          FN: req.session.firstname,
          LN: req.session.lastname,
          weather: weather.temperature.value,
        }).catch(err => console.log(err));
      })
    } else {
      res.render('login.ejs')
    }
})

或者如果你更舒服,你可以使用 async/await

try {
    const weather = await getWeather(location);
    console.log(`The temperature here is ${weather.temperature.value}`);

    res.render("admin-dashboard", {
        page_title: "admin-dashboard",
        FN: req.session.firstname,
        LN: req.session.lastname,
        weather: weather.temperature.value,
    });
} catch(err) {
    console.log(err);
}

请注意,我只是记录错误,您可以根据需要处理它们。

【讨论】:

  • 我已经根据您的有用回答编辑了我的问题以显示我做了什么,但它失败了...我把 .catch 放在错误的地方了吗?
  • 嗯,奇怪,我觉得还不错。您是否仍然收到 UnhandledPromiseRejectionWarning 或者您现在收到的实际错误被注销了?
  • 是的,我仍然遇到同样的错误,UnhandledPromiseRejectionWarning 一个。
  • 啊,我认为您在设置渲染对象的 weather 属性的值时遗漏了一些反引号,即它应该是 `${weather.temperature.value}` 而不仅仅是 ${weather.temperature.value} 或者在这种情况下您可以简单地一起删除大括号,因为您实际上并没有使用字符串插值。这是抛弃格式,因此捕获错误的位置。我已经更新了我的答案以反映固定版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多