Ben*_*oit 5 firebase google-cloud-functions google-cloud-firestore
我得到了以下云功能,效果很好。它正在侦听实时数据库中的更新并相应地更新 Firestore。
一切都很好,除非我的用户还不存在我的 Firestore 数据库。
这Unhandled rejection是我在 Google Cloud Functions 日志中看到的需要处理的地方。
所以看下面,在函数的缩短版本中,我db.collection("users").where("email", "==", email).get()如何停止函数向前移动并防止崩溃。
exports.updateActivities = functions.database.ref("delegates/{userId}/activities").onWrite((event) => {
//Here I set all the needed variable
return rtdb.ref(`delegates/${userId}/email`).once("value", snapshot => {
//Here I'm fine, email is always present.
})
.then(() => {
db.collection("users").where("email", "==", email).get()
//This is where I need to handle when there is not matching value, to stop moving forward.
.then(querySnapshot => {
querySnapshot.forEach(val => {
console.log("Found match in FireStore " + val.id);
firestoreId = val.id;
})
})
.then(() => {
//Here I start my update on Firestore
});
})
});
Run Code Online (Sandbox Code Playgroud)
您应该对从函数返回的每个可能被拒绝的承诺使用catch()。这会告诉 Cloud Functions 您处理了错误。从 catch() 返回的承诺将被解决。
通常,您从 catch() 记录错误,以便您可以在控制台日志中看到它:
return somePromise
.then(() => { /* do your stuff */ }
.catch(error => { console.error(error) })
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3353 次 |
| 最近记录: |