简体   繁体   中英

update Database on firebase cloud function too slow

I use firebase cloud function and firebase realtime database for my e-commerce.I have a problem on my function, when i run the function, i watch the realtime database and see changes made too late(10-15 sec). Am I wrong or missing somewhere?

firebase function:

app.post('/test', async (req:any, res:any) => {
  console.log("1")
  await iyzipay.checkoutForm.retrieve({
    locale: Iyzipay.LOCALE.TR,
    conversationId: '123456789',
    token: req.body.token
},  (err:any, result:any) => {
  if (result.paymentStatus = "SUCCESS") {
   admin.database().ref("tokens/"+req.body.token).once("value",async snap=>{
console.log("2")
    const userID = snap.child("who").val()
    admin.database().ref("users/"+userID+"/shopCart").remove()
    const snapshot =  await admin.database().ref("users/"+userID+"/copyShopCart/"+req.body.token).once("value")
    const array:any = []

   await snapshot.forEach(child  => {array.push(child)})
console.log("3")
      for await (const child of array) {
console.log("4")
        admin.database().ref("collections/"+child.key).once("value",async snap=>{

          var xs:number = snap.child("stock").child("xs").val()-(child.child("xs").val() || 0)
          var s:number = snap.child("stock").child("s").val()-(child.child("s").val() || 0)
          var m:number = snap.child("stock").child("m").val()-(child.child("m").val() || 0)
         
          admin.database().ref("collections/"+child.key+"/stock").update({xs:xs,s:s,m:m})
console.log("5")
      })
      }
console.log("6")
      await admin.database().ref("users/"+userID+"/myOrders").update(snapshot.val())//My orders

   })
 }
});
  await console.log("7")
res.redirect('http://localhost:4200/home')
res.end("...");
  });

  exports.widgets = functions.region('europe-west1').https.onRequest(app);

firebase function log:

在此处输入图像描述

In all Firebase Functions, we terminate the function when a value is returned or when a promise finishes and is returned. When you execute an asynchronous operation in a background triggered Cloud Function, you must return a promise, in such a way the Cloud Function waits until this promise resolves in order to terminate.

Typically this issue might happen because part of the code is executed in an asynchronous callback or Promise but a value has been returned or the end of the function was reached.

In order to make your function successfully completed, promises should be resolved so that it will not terminate the function without completing the work.

Syntax:

const Promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('new');
  }, 300);
});

Promise1
  .then(Resolved1, Rejected1)
  .then(Resolved2, Rejected2)
  .then(Resolved3, Rejected3);

For more information you can refer these StackOverflow thread1 , thread2 & thread3 and these documents as well document1 & document2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM