gpt4 book ai didi

javascript - 未处理的 promise 拒绝警告 : This error originated either by throwing inside of an async function without a catch block

转载 作者:数据小太阳 更新时间:2023-10-29 05:00:46 25 4
gpt4 key购买 nike

我的 Node-Express 应用出现以下错误

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)

至少可以说,我创建了一个看起来像这样的辅助函数

const getEmails = (userID, targettedEndpoint, headerAccessToken) => {
return axios.get(base_url + userID + targettedEndpoint, { headers: {"Authorization" : `Bearer ${headerAccessToken}`} })
.catch(error => { throw error})
}

然后我导入这个辅助函数

const gmaiLHelper = require("./../helper/gmail_helper")

然后像这样在我的 api 路由中调用它

router.get("/emailfetch", authCheck, async (req, res) => {
//listing messages in users mailbox
let emailFetch = await gmaiLHelper.getEmails(req.user._doc.profile_id , '/messages', req.user.accessToken)
.catch(error => { throw error})
emailFetch = emailFetch.data
res.send(emailFetch)
})

从我的 Angular 来看,我认为我是通过使用 catch block 来处理错误的。

问题:有人能解释一下为什么会出现错误吗?我该如何解决?

最佳答案

.catch(error => { throw error}) 是空操作。它会导致路由处理程序中出现未处理的拒绝。

this answer 中所述, Express 不支持 promises,所有拒绝都应该手动处理:

router.get("/emailfetch", authCheck, async (req, res, next) => {
try {
//listing messages in users mailbox
let emailFetch = await gmaiLHelper.getEmails(req.user._doc.profile_id , '/messages', req.user.accessToken)
emailFetch = emailFetch.data
res.send(emailFetch)
} catch (err) {
next(err);
}
})

关于javascript - 未处理的 promise 拒绝警告 : This error originated either by throwing inside of an async function without a catch block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53940043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com