我得到了以下云功能,效果很好。它正在侦听实时数据库中的更新并相应地更新Firestore。
一切都很好,除非我的用户不存在我的Firestore数据库。
我需要处理我在Google Cloud Functions日志中看到的Unhandled rejection
。
所以请看下面,在缩短版本的函数中,对于我的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
});
})
});
你应该使用catch()对你从函数中返回的每个可能被拒绝的承诺。这告诉Cloud Functions您处理了错误。从catch()返回的promise将被解决。
通常,您从catch()记录错误,以便在控制台日志中看到它:
return somePromise
.then(() => { /* do your stuff */ }
.catch(error => { console.error(error) })