为什么我不能在Firebase中查询子集合或子集合中的文档?

时间:2020-04-10 19:30:59

标签: javascript firebase react-native google-cloud-firestore

我在此查询中尝试从子集合中获取文档,但未获取任何内容,也未显示任何错误。可能是Firebase方面的问题,还是我在这里做错了什么?

const db = firebase.firestore();
let ref = db.collection('myCollection').doc('EzGNfNl63LQvjdauFWosG08Ishj2').collection('privat').doc('privat');
        ref.get()
            .then(doc => {  //it doesnt continue to next line, just executes this one
                if (doc.exists) {
                    console.log("Doc exists:", doc.data())
                }else{
                    console.log("Doc deostn exists:")
                }

            }).catch(error => {
                console.log('Error getting restaurant data: ', error);
            })

但是,如果我尝试仅查询集合中的文档,那么它将起作用:

let ref = db().collection('myCollection').doc('EzGNfNl63LQvjdauFWosG08Ishj2');

编辑:添加了我的数据库的屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:0)

更改此:

const db = firebase.firestore.
let ref = db().collection('myCollection').doc('EzGNfNl63LQvjdauFWosG08Ishj2').collection('privat').doc('privat');
        ref.get()
            .then(doc => {  //it doesnt continue to next line, just executes this one
                if (doc.exists) {
                    console.log("Doc exists:", doc.data())
                }else{
                    console.log("Doc deostn exists:")
                }

            }).catch(error => {
                console.log('Error getting restaurant data: ', error);
            })

对此:

const db = firebase.firestore();
let ref = db.collection('myCollection').doc('EzGNfNl63LQvjdauFWosG08Ishj2').collection('privat').doc('privat');
        ref.get()
            .then(doc => {  //it doesnt continue to next line, just executes this one
                if (doc.exists) {
                    console.log("Doc exists:", doc.data())
                }else{
                    console.log("Doc deostn exists:")
                }

            }).catch(error => {
                console.log('Error getting restaurant data: ', error);
            })

firestore()是一个方法,而db是一个常量。