简体   繁体   English

更新firebase云function数据库太慢

[英]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). 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: 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: firebase function 日志:

在此处输入图像描述

In all Firebase Functions, we terminate the function when a value is returned or when a promise finishes and is returned.在所有 Firebase 函数中,我们在返回值或 promise 完成并返回时终止 function。 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. 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.通常会发生此问题,因为部分代码在asynchronous回调或 Promise 中执行,但已返回值或到达 function 的末尾。

In order to make your function successfully completed, promises should be resolved so that it will not terminate the function without completing the work.为了使您的 function 成功完成,应解决承诺,使其不会在未完成工作的情况下终止 function。

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有关更多信息,您可以参考这些 StackOverflow thread1thread2thread3以及这些文档以及document1document2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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