【发布时间】: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')
}
})
【问题讨论】:
-
这里似乎已经回答了这个问题:What is an unhandled promise rejection?.
-
这能回答你的问题吗? What is an unhandled promise rejection?
标签: javascript node.js promise