Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

I am new in NodeJS and Firebase cloud function, here is my code in my firebase cloud function:

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

})

as you can see, I have returned a promise in return admin.auth().setCustomUserClaims(moderatorID , but why when I deploy using firebase deploy i still have warning in the terminal that said

warning : Expected to return a value at the end of async arrow function consistent-return

like image 354
Agung Laksana Avatar asked Oct 12 '25 20:10

Agung Laksana


2 Answers

you are missing return value outside try catch block, in case there's exception there's no return from the function, modify the function as follows

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
})
like image 199
ked Avatar answered Oct 14 '25 09:10

ked


The linter is informing you that there is a way for your code to run that doesn't return a value. In your code above, this is because no value is returned when your try-catch block catches an exception.

However, with the code as it is above, the try-catch block won't do anything useful because the only two errors that would trigger it would be not initialising the Admin SDK or some syntax error.

To catch the errors from the setCustomUserClaims operation using a try-catch block, you must make use of await. Also, as setCustomUserClaims doesn't resolve to a value, there is no need to return it when used this way.

Also, your current code doesn't handle the case where a moderator is deleted and their permission should be revoked. This can be added by changing to onWrite instead of onUpdate (which should have been onCreate) and checking the value of change.after.exists. If we add this functionality, it also makes sense to rename the function to dbModeratorsUpdateClaims or similar.

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

Alternatively, not using the async/await syntax, you can return the promise itself and chain to it's rejection handler.

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);
    });
})
like image 32
samthecodingman Avatar answered Oct 14 '25 10:10

samthecodingman