Search code examples
angularfirebasegoogle-cloud-firestoreionic4

How Can I assign the SnapshotChanges in a Observable Firebase Variable?


I want to load all the data of a document, but I can not access the id of the document. When i print the object this.ressobj the id is visible, but when i put the ressobj.id_doc in the card the id id of the document is not visible.

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 CODE

<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>

Thanks for your help


Solution

  • Not tested but I've used something like that for my angularfire2 projects:

    Getting a document:

    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
    

    Getting a collection:

    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