0

I'm not able to get the ID of the document when I query a Firestore Database this way :

Could you give me some help ?

import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection  } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
export interface Item { name: string; }

@Component({
  selector: 'app-annonces-contactees',
  templateUrl: './annonces-contactees.page.html',
  styleUrls: ['./annonces-contactees.page.scss'],
})
export class AnnoncesContacteesPage implements OnInit {
  private annoncesCollection: AngularFirestoreCollection<Item>;
  annonces: Observable<Item[]>;
    constructor(private afs: AngularFirestore) {
      this.annoncesCollection = afs.collection('annonces', ref => ref.where('prix', '>=', 1000000))
      this.annonces = this.annoncesCollection.valueChanges();
   }

  ngOnInit() {
}

}
4
  • Try this.. this.annoncesCollection..snapshotChanges().pipe( map(actions => actions.map(a => { const data = a.payload.doc.data() as Item; const id = a.payload.doc.id; return { id, ...data }; })) Commented Feb 24, 2021 at 12:11
  • Thanks but how to display it in my html code ? Now I'm using this to get results : <li *ngFor="let annonce of annonces | async"> {{ annonce.prix }} </li> Commented Feb 24, 2021 at 12:16
  • here is the example 'github.com/angular/angularfire/blob/master/docs/firestore/…' Commented Feb 24, 2021 at 12:18
  • Are you sure of your code ? I get errors Commented Feb 24, 2021 at 12:27

1 Answer 1

1

I am going to give you an example of how I dot it: Let us suppose I have collection of hospitals and each hospital has its name,phone and location.

  constructor(private firestore:AngularFirestore){}
  hospitalsArray=[];
  ngOnInit(){
      this.firestore.collection("hospitals").snapshotChanges().subscribe((data) => {
        this.hospitalsArray = data.map(e => {
          return { id: e.payload.doc.id, location: e.payload.doc.data()["location"], number: e.payload.doc.data()["phone"], name: e.payload.doc.data()["name"]}
        })
  }

"hospitals" is the name of the collection and this id is the id of the document.

So if you want to display in the html file

<ion-item *ngFor="let hospital of hospitalsArray">
<ion-label>{{hospital.name}}</ion-label>
</ion-item>
14
  • Hi, Do you know how can I sort data by ID ? When I don't use the where condition I get the result sorted by ID but the results are sorted randomly whis the Where condition.. Any idea ? Commented Feb 25, 2021 at 16:10
  • Actually I have never tried it, I don't know if there is a build in function used for arrays for sorting , if not try using some sorting algorithms. And also i am going to try to search for it. Commented Feb 25, 2021 at 17:14
  • But why would you want to sort an array by id?? Commented Feb 25, 2021 at 17:15
  • I need sort an array buy date, but as I'm using "Where" it't not allowed to sort data using one other filed ("date"). So I try to sort my data by ID or Date in template directly. Thanks for your time. Commented Feb 25, 2021 at 18:55
  • You can after filling the array you used to fill your data. To sort it depending on the field you want. Anyway what do you mean by "you are using where"?? Commented Feb 26, 2021 at 18:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.