Why I cant query sub-collection or a document from a sub-collection in Firebase?

不问归期 提交于 2020-04-17 21:25:32

问题


I have this query where I am trying to get a document from a sub-collection but it doesnt get anything, neither it shows any error:

const db = firebase.firestore(9;
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);
            })

However, if I try to query only a document from the collection then it works:

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

EDIT: Screenshot added


回答1:


Change this:

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

Into this:

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() is a method and db is a constant.



来源:https://stackoverflow.com/questions/61147374/why-i-cant-query-sub-collection-or-a-document-from-a-sub-collection-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!