【问题标题】:Why I cant query sub-collection or a document from a sub-collection in Firebase?为什么我无法从 Firebase 的子集合中查询子集合或文档?
【发布时间】:2020-04-10 19:30:59
【问题描述】:

我有这个查询,我试图从子集合中获取一个文档,但它没有得到任何东西,也没有显示任何错误。这可能是 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');

编辑:添加了我的数据库的屏幕截图

【问题讨论】:

  • 请编辑问题以显示您尝试获取的文档的屏幕截图。另外请清楚您的意思是“它不会继续到下一行,只是执行这一行”。这里到底发生了什么?
  • 那段代码是写的吗?您似乎开始在第一行使用点运算符,然后移至变量声明。如果是这样,那可能是你的问题。
  • 您确定文档与集合同名吗?
  • @DougStevenson 在调试时我在所有这些行中设置了一个断点,我可以遍历每一行直到达到.then(doc => { 然后当我按下 F8 时它什么也不做。跨度>
  • @PeterHaddad 我很确定,我是这样命名它们的,然后我通过选择不输入自己来复制两者的名称。

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


【解决方案1】:

改变这个:

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 是一个常量。

【讨论】:

  • 这个问题给出了一个使用db() 的例子。这可能只是一个错字。
  • @bmmf 如果你检查你的图片,EzGNfNl63LQvjdauFWosG08Ishj2 似乎不是myCollection 下的直接文件。
  • @PeterHaddad 检查更新的截图,nvm 上一张。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 2020-03-10
  • 2020-07-26
  • 1970-01-01
  • 2020-08-28
相关资源
最近更新 更多