如何在可观察的Firebase变量中分配SnapshotChanges?

时间:2019-04-24 16:56:54

标签: angular firebase google-cloud-firestore ionic4

我想加载文档的所有数据,但无法访问文档的ID。当我打印对象this.ressobj时,id是可见的,但是当我将ressobj.id_doc放在卡中时,文档的ID则不可见。

this.ResColeccion = this.resser.getSpecificlistRestaurants(this.id);
     this.ResObservable$ = this.ResColeccion.snapshotChanges().subscribe(usuariolista =>{
    this.ressobj=usuariolista.map(res =>{
 return{
   nombre:res.payload.doc.data().nombre,
   logo:res.payload.doc.data().logo,
   descripcion:res.payload.doc.data().descripcion,
   portada:res.payload.doc.data().portada,
   idadmin:res.payload.doc.data().idadmin,
   id_doc:res.payload.doc.id
 }
 
        });
        console.log(this.ressobj);
        
      });

HTML代码

<ion-card *ngFor="let ressobj of ResObservable$ | async " [routerLink]="['/opcionesrestaurante',ressobj.id_doc]"  >
    
       
      
        <img src="{{ressobj.portada}}">
  
       
     <ion-card-content>
       
    
        <ion-card-title>
            <h1>{{ressobj.nombre}}</h1>
        </ion-card-title> 
      <p>{{ressobj.descripcion}}</p>
      </ion-card-content>
    </ion-card>

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

未经测试,但我在我的angularfire2项目中使用了类似的方法:

获取文档

const ResObservable$ = () => {
        return this.ResColeccion
          .snapshotChanges()
          .pipe(
            map(actions => {
              const data = actions.payload.data() as any;
              const uid = actions.payload.id;
              return { uid, ...data};
            })
          );
    }

ResObservable$.subscribe(item => console.log(item)) // or use async

获取收藏集:

const ResObservable$ = () => {
    return this.ResColeccion
      .snapshotChanges()
      .pipe(
        map(actions => {
          return actions.map(a => {
            const data = a.payload.doc.data() as any;
            const uid = a.payload.doc.id;
            return { uid, ...data };
          });
        })
      );
}

ResObservable$.subscribe(items => console.log(items)) // or use async pipe