簡體   English   中英

(節點:52585)UnhandledPromiseRejectionWarning:未處理的 promise 拒絕

[英](node:52585) UnhandledPromiseRejectionWarning: Unhandled promise rejection

我有一個 function ,我使用導入的 npm package current-weather 根據該位置的經度和緯度獲取某個位置的天氣。 我以前從未使用過 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 (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 (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. (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')
    }
})

您沒有getWeather function 引發的錯誤。 您可以將.catch添加到 function,例如

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

請注意,我只記錄錯誤,您可以根據需要處理它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM