Google Cloud Functions中未处理的拒绝 [英] Unhandled Rejection in Google Cloud Functions

查看:32
本文介绍了Google Cloud Functions中未处理的拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了下面的云功能,效果很好.它正在监听实时数据库中的更新,并相应地更新Firestore.

I got the following cloud function which works great. It's listening for an update in the real-time database and update Firestore accordingly.

一切都很好,除非我的用户还不存在我的Firestore数据库.

Everything is fine, except when my user does not exist yet my Firestore database.

我需要处理在Google Cloud Functions日志中看到的未处理的拒绝.

This where I need to deal with Unhandled rejection that I see in the Google Cloud Functions log.

因此,请参见以下功能的简化版本中的 db.collection("users").where("email","==",email).get()如何停止该功能以继续前进并防止崩溃.

So see below, in the shortened version of the function, for my db.collection("users").where("email", "==", email).get() how to stop the function to move forward and prevent the crash.

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
                });
        })
});

推荐答案

您应使用

You should use catch() on every promise that you return from your function that could be rejected. This tells Cloud Functions that you handled the error. The promise returned from catch() will be resolved.

通常,您从catch()记录错误,以便可以在控制台日志中看到它:

Typically you log the error from catch() so you can see it in the console logs:

return somePromise
.then(() => { /* do your stuff */ }
.catch(error => { console.error(error) })

这篇关于Google Cloud Functions中未处理的拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆