我最近開始使用 Firebase Cloud Functions,我對如何適當地終止這個 HTTP 函式有點困惑:
exports.UpdateUserInfo= functions.https.onRequest( async (request, response) => {
try{
//Make a read call to Realtime DB
const snapshot = await admin.database().ref('/UserData').get()
if (snapshot.exists() == false) {
response.send("No users found")
return null
}
//All of the remaining code within the scope of the try block executes
functions.logger.log("Wait, this function should have ended")
let updateUserInfo = await userInfoUpdater(response)
}
catch{
functions.logger.info(error);
response.status(500).send("ERROR")
}
})
根據我的閱讀,終止 HTTP 函式的正確方法是通過回應物件發送回應。但是,似乎除非我包含對return null的最終呼叫,否則該函式會繼續執行超出其預期壽命。更糟糕的是,該函式終止并仍然允許執行額外的網路呼叫,這使得事情變得非常不可預測和無組織(參見日志)。一旦滿足條件,我想阻止該功能繼續。回傳 null 是確保正確終止的最佳方法,還是我遺漏了什么?

uj5u.com熱心網友回復:
呼叫response.send()不會立即終止函式。它只是向 Cloud Functions 發出信號,該函式應該在當前代碼塊回傳后關閉,無論是通過 return 陳述句還是從函式塊的末尾掉下來。如果函式沒有以這兩種方式之一回傳,則 Cloud Functions 將超時或出現其他問題,因為在發送回應后 CPU 很快就會被限制。
本質上,發送回應應該是函式在回傳之前所做的最后一件事。其他任何事情都容易出錯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/400048.html
標籤:javascript 火力基地 火力实时数据库 谷歌云功能
上一篇:如何從url中的id引數創建頁面
