首页 > 解决方案 > 为什么我仍然有警告:即使我已经返回了一个承诺,也应该在云函数中的异步箭头函数的末尾返回一个值?

问题描述

我是 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

警告:预期在异步箭头函数一致返回的末尾返回一个值

标签: node.jsfirebasegoogle-cloud-functions

解决方案


你在try catch块之外缺少返回值,如果出现异常,函数没有返回,修改函数如下

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

推荐阅读