查询集合中的Firebase集合无法获取文档

时间:2020-08-18 22:10:20

标签: angular typescript firebase google-cloud-firestore

我正在尝试使用Firebase获取我的所有文档(如果它们基于查询)。 所以我的firebase结构是:

enter image description here

我尝试了我的方法,并使用empthy来检查是否有文档,但是我总是得到TRUE,但是我认为该路径是错误的,因为我在“ ListaFavorite”中有2个文档。

 firebase
      .firestore()
      .collection(`users`)
      .doc(`${this.MyUser.uid}`)
      .collection("ListaFavorite")
      .get()
      .then((result) => {
        const check = result.empty;
        console.log("DATA is: " + check);
      });

因此,基于查询,我想从那里获取所有文档并显示在我的html页面中。有什么帮助吗?谢谢。

2 个答案:

答案 0 :(得分:1)

您的ListaFavorite是一个字段,而不是子集合。这意味着您必须将其作为字段来解决,而不是尝试将其作为子集合来加载。

firebase
  .firestore()
  .collection(`users`)
  .doc(`${this.MyUser.uid}`)
  .get()
  .then((doc) => {
    const check = doc.data().ListaFavorite;
    console.log("DATA is: " + check);
  });

答案 1 :(得分:1)

ListaFavorite${this.MyUser.uid}文档的字段,而不是的子集合。根据您的用法,您可以通过+ Start collection按钮对其进行更改,使其成为子集合,也可以使用以下命令进行检查:

firebase
      .firestore()
      .collection('users')
      .doc(`${this.MyUser.uid}`)
      .get()
      .then((doc) => {
        const check = doc.get('ListaFavorite');
        console.log("DATA is: " + check);
      });