简体   繁体   English

如何检查 Firestore 查询是否为空或我尝试获取的文档不存在?

[英]How to check if Firestore query is empty or document that I am trying to fetch does not exist?

I am trying to fetch a document in Cloud Firestore based on a field ID, but that ID cannot be found, how to check that because it is throwing an error?我正在尝试根据字段 ID 在 Cloud Firestore 中获取文档,但找不到该 ID,如何检查是否因为它引发错误?

    getMenu = () => {
    try {
        this.setState({
            loading_menu: true
        })

        var ref = firebase.firestore().collection('restaurants_menu')
            .where("rest_id", "==", this.getSelected.rest_id)
            .limit(this.state.limit_menu)

        ref.onSnapshot((querySnapshot => {
            var menu = querySnapshot.docs.map(doc => { return { ...doc.data(), id: doc.id } });
            var lastVisibleMenu = menu[menu.length - 1].rest_id;
            this.setState({
                menu: menu,
                lastVisibleMenu: lastVisibleMenu,
                loading_menu: false,
            });
        }));
    }
    catch (error) {
        console.log("SOMETHING HAPPENED, WE COULD NOT FETCH restaurants_menu:  ", error);
    }
}

If your snapshot handler is getting invoked, then there was no error.如果您的快照处理程序被调用,则没有错误。 Just check the length of the querySnapshot.docs array to find out how many documents were matched by the query.只需检查querySnapshot.docs数组的长度即可找出查询匹配了多少文档。 It's not an error to match 0 documents - you should expect that could be the case.匹配 0 个文档不是错误 - 您应该预料到可能是这种情况。

If you want to handle possible errors, you should be passing more than a single snapshot handler.如果您想处理可能的错误,您应该传递多个快照处理程序。 The documentation for onSnapshot() suggests that you should pass a single observer object with various properties for each type of handler function. onSnapshot()的文档建议您应该为每种类型的处理程序函数传递具有各种属性的单个观察者对象。

ref.onSnapshot({
    next: snapshot => {
        // your snapshot handler code
    },
    error: error => {
        // your error handling code
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 JS 中的 firestore(不是文档)中是否存在集合 - How do I check if collection exist in firestore (not document) in JS 如果 firestore 文档不存在,如何抛出错误? - How to throw an error if firestore document does not exist? Ionic firestore如何检查firestore中是否存在文档ID - Ionic firestore how to check document id exist in firestore or not 我正在尝试在 Firestore 中查询时间戳 - I am trying to query timestamps in firestore 如何检查文档参考的javascript数组中是否存在Firestore文档参考? - How to check if a Firestore Document reference exist in a javascript array of Document references? 我正在尝试从 ReactJS 更新 Firestore 文档中的字段 - I am trying to update a field inside a document in Firestore from ReactJS Firestore - 如何根据特定字段检查文档是否存在 - Firestore - How to check if a document exist based on a specific field 我在 firestore 中查询文档并尝试将其添加到数组中,但数组返回空 - I query firestore for a document and try to add it to an array, but the array returns empty 查询 Firestore 以检查集合和文档 - query to firestore to check collection and document 我正在尝试检查给定值是否作为对象数组中的键存在 - I am trying to check if given value exist as key in array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM