我得到了下面的云函数,它工作得很好。它监听实时数据库中的更新,并相应地更新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
});
})
});
发布于 2017-12-29 21:08:39
你应该对你的函数返回的每一个可能被拒绝的承诺使用catch()。这将告诉Cloud Functions您已处理了错误。将解析catch()返回的promise。
通常,您从catch()中记录错误,以便可以在控制台日志中看到它:
return somePromise
.then(() => { /* do your stuff */ }
.catch(error => { console.error(error) })
发布于 2021-06-20 05:38:47
我在suppressUnhandledRejections
中使用了bluebird
来解决这个问题:
import Promise from 'bluebird';
function pool_push(pool, promise)
{
// In Google Cloud there is a change to crash at `unhandledRejection`.
// The following trick, basically, tells not to emit
// `unhandledRejection` since `.catch` will be attached
// a bit latter.
const tmp = Promise.resolve(promise);
tmp.suppressUnhandledRejections();
pool.items.push(tmp);
}
export default pool_push;
https://stackoverflow.com/questions/48028366
复制相似问题