Home > front end >  Call setTimeout at Firebase Cloud Functions onCreate
Call setTimeout at Firebase Cloud Functions onCreate

Time:02-26

I'm using following code in order to call a function with setTimeout, but I'm not really into JS and Firebase Cloud Functions.

However my logs are showing following error: "Function returned undefined, expected Promise or value" "Exception from a finished function: Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string."

This is my code:

function updateDoc(id) {
    admin.firestore().collection('Orders').doc(id).update({status: 2}).then(() => {
        console.log("Document successfully updated!");
    })
    .catch((error) => {
        console.error("Error updating document: ", error);
    });
}

exports.updateField = functions.firestore
    .document('/Orders/{id}')
    .onCreate((snapshot, context) => {
        const id = context.params.id;
      setTimeout(updateDoc.bind(id), 30000) 
    });

Thanks for any kind of help.

CodePudding user response:

There are several problems with your code:

  • You should pass the id as a "simple" argument when calling the updateDoc()method.
  • You need to return a Promise or a value in your Cloud Function to indicate to the Cloud Function platform that the Cloud Function has completed. You'll find more details in the doc and in this video series.

In addition, using setTimeout in a Cloud Function shall be done with care, see these SO answers. You could probably re-architect your solution in order to avoid the setTimeout. If you share more info on why you use it, the community may help you.

  • Related