Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase cloud functions onCreate took 3 minutes

Below is Cloud functions code and functions log with Timestamp , it took 3 minutes of time to update in FireStore table, I was trying with change of Node JS runtime 16 to 10 , But still took long to update, Any help would be really appreciated

2023-03-23 10:30:45.849 GMT creationBrandCode 8r93ujy5wjau Function execution started
2023-03-23 10:30:46.144 GMT creationBrandCode 8r93ujy5wjau Function execution took 294 ms, finished with status: ‘ok’
2023-03-23 10:33:13.540 GMT creationBrandCode 8r93ujy5wjau { ReturnCode: 200,
2023-03-23 10:33:13.540 GMT creationBrandCode 8r93ujy5wjau Message:
2023-03-23 10:33:13.540 GMT creationBrandCode 8r93ujy5wjau ‘Updated brand for doc_id brand_codeqE5vYqLTGpxq600z7IyL BR106’ }


//unique code creation
exports.creationBrandCode = functions.firestore
  .document("brands/{id}")
  .onCreate(async (snap, context) => {
    const brand_doc_id = context.params.id;
    let BrandCode;
   //Getting unique code collection
    //const UniQueID = db.collection("md_unique_code").doc("brand_code");
    //UniQueID.get()
      //.then((snap_brand) => {
//if (snap_brand.exists) {

    //Generating unique code 
          BrandCode = snap_brand.data().Unique_ID;
          let split = BrandCode.split("BR");
          let X = parseInt(split[1]) + 1;
          const result = pad(X, 3);
          let Unique_IDNew = "BR" + result;

       //Updating unique code md_unique_code collection next time 
          UniQueID.update({
            Unique_ID: Unique_IDNew,
          });

       //Updating unique code data created collection 
          db.collection("brands")
            .doc(brand_doc_id)
            .update({
              brand_code: BrandCode,
            })
            .then(() => {
              //Return result console log 
              //return console.log({
                ReturnCode: 200,
                Message:
                  "Updated brand for doc_id brand_code" +
                  brand_doc_id +
                  " " +
                  BrandCode,
              });
            })
            .catch((error) => {
            //Return result console log 
              //return console.log({
                ReturnCode: 201,
                Message: error.message,
              });
            });
        } else {
          //Return result console log 
          //return console.log({
            ReturnCode: 201,
            Message:
              "brand_code document not found in md_unique_code collection",
          //});
        //}
     // })
      //.catch((error) => {
       //Return result console log 
        //return console.log({
         // ReturnCode: 201,
         // Message: error.message,
        //});
     // });
  //});

//Return result console log  to find result log on the Cloud Console

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You don’t correctly manage the life cycle of the Cloud Function. You need to use await with asynchronous methods and return a Promise or an object when all the asynchronous work is done. See here in the doc for more details.

Your code is not very clear and full of commented lines so I cannot point all the problems but you should do along the following lines:

exports.creationBrandCode = functions.firestore
    .document("brands/{id}")
    .onCreate(async (snap, context) => {

        try {
            const brand_doc_id = context.params.id;
            let BrandCode;

            // ...

            const UniQueID = db.collection("md_unique_code").doc("brand_code");

            //Updating unique code md_unique_code collection next time 
            await UniQueID.update({  // <= See await here
                Unique_ID: Unique_IDNew,
            });

            //Updating unique code data created collection 
            await db.collection("brands")  // <= See await here
                .doc(brand_doc_id)
                .update({
                    brand_code: BrandCode,
                })

            return null; // See return here. No need to return a 200 code, it is not an HTTPS Cloud Function
        } catch (error) {
            console.log(error);
            return null;
        }

    });

If after cleaning your code, making it easier to understand and adapting it as shown above you still encounter some problems, then post the new code in your question and we will be able to help you more precisely.


I would also kindly suggest you read the help on "How do I ask a good question?".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading