javascriptfirebasegoogle-cloud-firestorefirebase-authenticationgoogle-cloud-functions

Firebase Cloud Functions onCreate auth not working correclty


I have a Cloud Function that triggers when a user creates an account and updates a value inside my Firestore.

Everything worked perfectly before, but since this morning (I didn't change a single line of code in my functions) it stopped updating the value in the Firestore.

The weird thing is that onDelete is working.

Here are both functions:

exports.accountCreate = functions.auth.user().onCreate((user) => {
    console.log(user.uid);
    const userCounterRef = admin.firestore().collection("data").doc("userCounter");

    userCounterRef.update({
        userCounter: admin.firestore.FieldValue.increment(1),
    });
});

exports.accountDelete = functions.auth.user().onDelete((user) => {
    console.log(user.uid);
    const userCounterRef = admin.firestore().collection("data").doc("userCounter");

    userCounterRef.update({
        userCounter: admin.firestore.FieldValue.increment(-1),
    });
});

I don't see any errors in my logs:

enter image description here

The only thing that looks weird are the two logs that I see quite often:

Exception from a finished function: Error: 4 DEADLINE_EXCEEDED: Deadline exceeded

Function returned undefined, expected Promise or value

What am I missing here?


Solution

  • Take note of the message:

    Function returned undefined, expected Promise or value

    Cloud Functions must return a promise that resolves when all async work is complete. Right now, you are ignoring the promises returned by update. This means you have no guarantee that the work will complete - at best it will finish randomly.

    You can fix this easily by returning the promise from both functions:

    userCounterRef.update(...);
    

    I suggest already reading the documentation to understand this better. You must absolutely understand how JS promises work and use them correctly in order work with Cloud Functions effectively.