[英]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). 我錯了還是錯過了某個地方?
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 日志:
在所有 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.
通常會發生此問題,因為部分代碼在asynchronous
回調或 Promise 中執行,但已返回值或到達 function 的末尾。
為了使您的 function 成功完成,應解決承諾,使其不會在未完成工作的情況下終止 function。
句法:
const Promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('new');
}, 300);
});
Promise1
.then(Resolved1, Rejected1)
.then(Resolved2, Rejected2)
.then(Resolved3, Rejected3);
有關更多信息,您可以參考這些 StackOverflow thread1 、 thread2和thread3以及這些文檔以及document1和document2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.