【问题标题】:why I still have warning: Expected to return a value at the end of async arrow function in cloud function, even though I have returned a promise?为什么我仍然有警告:即使我已经返回了一个承诺,也应该在云函数中的异步箭头函数末尾返回一个值?
【发布时间】: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


【解决方案1】:

你在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
})

【讨论】:

    【解决方案2】:

    linter 通知您,您的代码有一种不返回值的运行方式。在您上面的代码中,这是因为当您的 try-catch 块捕获异常时没有返回值。

    但是,使用上面的代码,try-catch 块不会做任何有用的事情,因为触发它的仅有两个错误不会初始化 Admin SDK 或一些语法错误。

    要使用try-catch 块捕获来自setCustomUserClaims 操作的错误,您必须使用await。此外,由于setCustomUserClaims 不会解析为值,因此使用这种方式时无需返回它。

    此外,您当前的代码不处理版主被删除并且应撤销其权限的情况。这可以通过更改为onWrite 而不是onUpdate(应该是onCreate)并检查change.after.exists 的值来添加。如果我们添加此功能,将功能重命名为 dbModeratorsUpdateClaims 或类似名称也是有意义的。

    exports.dbModeratorsUpdateClaims = functions.firestore.document('moderators/{moderatorID}').onWrite(async (change,context) => { // <-- changed to onWrite
    
        // grants/revokes custom claim of the related moderator
    
        try {
    
            const moderatorID = context.params.moderatorID        
            const isModerator = change.after.exists // if deleted, revokes permission
    
            await admin.auth().setCustomUserClaims(moderatorID, { // <-- added await
                moderator: hasModeratorPermissions
            });
    
        } catch(error) {
            console.log(error);
        }
    })
    

    或者,不使用async/await 语法,您可以返回承诺本身并链接到它的拒绝处理程序。

    exports.dbModeratorsUpdateClaims = functions.firestore.document('moderators/{moderatorID}').onWrite((change,context) => { // <-- changed to onWrite
    
        // grants/revokes custom claim of the related moderator
    
        const moderatorID = context.params.moderatorID
        const isModerator = change.after.exists // if deleted, revokes permission
    
        return admin.auth().setCustomUserClaims(moderatorID, {
            moderator: isModerator
        })
        .catch((error) {
            console.log(error);
        });
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-12
      • 2022-11-17
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多