【发布时间】:2019-12-26 22:22:09
【问题描述】:
我是 NodeJS 和 Firebase 云功能的新手,这是我在 Firebase 云功能中的代码:
exports.dbModeratorsOnCreate = functions.firestore.document('moderators/{moderatorID}').onUpdate(async (change,context) => {
// grant custom claim to the newly created moderator
try {
const moderatorID = context.params.moderatorID
return admin.auth().setCustomUserClaims(moderatorID, {
moderator: true
})
} catch(error) {
console.log(error)
}
})
如您所见,我已经在 return admin.auth().setCustomUserClaims(moderatorID 中返回了一个承诺,但是为什么当我使用 firebase deploy 进行部署时,我仍然在终端中显示警告
警告:预计在异步箭头的末尾返回一个值 函数一致返回
【问题讨论】:
-
如果您的
catch块中捕获到异常,您希望返回什么?错误消息表明您需要在每个代码路径中都有一个返回值。 -
@DougStevenson 有可能什么都不做吗?我的意思是,我只想记录错误,我可以在云功能控制台中注意到错误。如果可以删除警告我该怎么办?
-
如果您什么都不做,则返回 null。关键是要明确告诉 TypeScript 你已经考虑了所有代码路径,这样无论发生什么,结果都是预期的,而不是任意或偶然的。
标签: node.js firebase google-cloud-functions