简体   繁体   English

如何从集合 firebase 中获取文档中的文档?

[英]How can i get the doc inside doc from collection firebase?

Hi?你好? How can i get the documents inside "ListaFavorite"?如何获取“ListaFavorite”中的文档?

I can get all other documents except this list, I only get [object object], it need to pipe again the documents or i dont know.我可以获取除此列表之外的所有其他文档,我只获取 [object object],它需要再次 pipe 文档或者我不知道。

Here is what i use:这是我使用的:

fav-page.html最喜欢的页面。html

 <ion-col *ngFor="let fav of this.favItems | async">        
          <ion-card>
            {{ fav.ListaFavorite}}
          </ion-card>
</ion-col>

UPDATE:更新:

async addFavorites(
    uniqID: string,
    favorite_title: string,
    favorite_description: string,
    favorite_image: string,
    favorite_tag: string,
    favorite_stoc: boolean,
    favorite_reducere: number,
    favorite_price: number,
    favorite_userId: string
  ): Promise<void> {
    this.MyUser = firebase.auth().currentUser;
    const { uid } = this.MyUser;

    const favoriteObj = {};
    favoriteObj[uniqID] = {
      favorite_title: favorite_title,
      favorite_description: favorite_description,
      favorite_image: favorite_image,
      favorite_tag: favorite_tag,
      favorite_stoc: favorite_stoc,
      favorite_reducere: favorite_reducere,
      favorite_price: favorite_price,
      favorite_userId: favorite_userId,
      isFavorite: true,
    };

 this.favItems = this.firestore
      .collection<User>("users", (ref) => ref.where("uid", "==", `${uid}`))
      .snapshotChanges()
      .pipe(
        map((actions) =>
          actions.map((a) => {
            const data = a.payload.doc.data() as User;
            const id = a.payload.doc.id;

            return { id, ...data };
          })
        )
      );

}

This way i can get the uniqueID of the object so i can simple do: console.log(this.favItems.ListaFavorite[uniqID].favorite_description);这样我可以得到 object 的唯一 ID,所以我可以简单地做: console.log(this.favItems.ListaFavorite[uniqID].favorite_description);

but i get this error:但我收到此错误:

TypeError: Cannot read property 'bayonete-QSrpS5021' of undefined

The thing is.. i already have the product in firebase, but it can't find the fields?问题是.. 我已经在 firebase 中有产品,但是找不到字段?

What is the next step?你下一步怎么做? xd xd

ListaFavorite is a map type field. ListaFavorite 是一个 map 类型字段。 It contains other maps, not documents.它包含其他地图,而不是文档。 In your code, you should treat it like a normal JavaScript object with properties whose names are the what you see in the console.在您的代码中,您应该将其视为普通的 JavaScript object ,其属性的名称与您在控制台中看到的相同。 You will need to know the names of the nested fields in that map in order to use them.您需要知道 map 中嵌套字段的名称才能使用它们。 For example:例如:

fav.ListaFavorite['bayonete-XXXX'].favorite_description

If you don't know the names in the map, you're going to have a hard time trying to figure out how to find the nested values.如果您不知道 map 中的名称,您将很难弄清楚如何找到嵌套值。

Hi i finally found a solution to my problem.嗨,我终于找到了解决我的问题的方法。 So basically, i just declared properties inside my function then i easy called them in html by the propery like this:所以基本上,我只是在我的 function 中声明了属性,然后我很容易在 html 中通过如下属性调用它们:

checkFavorite() {
    this.MyUser = firebase.auth().currentUser;

    const { uid } = this.MyUser;
    const data = this.MyUser.email;
    const myDoc = { uid };

   this.favItems = this.firestore
      .collection<User>("users", (ref) => ref.where("uid", "==", `${uid}`))
      .snapshotChanges()
      .pipe(
        map((actions) =>
          actions.map((a) => {
            const data = a.payload.doc.data() as User;

            return {
              id: a.payload.doc.id,
              Title: a.payload.doc.data().ListaFavorite[`bayonete162bcaca`].favorite_title,
              Description: a.payload.doc.data().ListaFavorite[`bayonete162bcaca`].favorite_description,
            };
          })
        )
      );
  }

And in HTML:在 HTML 中:

  <ion-col size-lg="6" size-md="6" size-sm="6" size-xs="12" *ngFor="let fav of this.favItems | async">
          <ion-card>
            {{  fav.Title }}
          </ion-card>
  </ion-col>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM