Search code examples
angulargoogle-cloud-firestoreangular7

How to use orderBy with snapshotChanges() in Firestore Angular7


I want to orderBy 'fullname' of 'employee' document in firestore using this code.

In service.ts

 getEmployees() {   
    return this.firestore.collection('employee').snapshotChanges();
  }

In Component.ts

  ngOnInit() {
            this.service.getEmployees().subscribe(actionArray => {
           let array = actionArray.map(item => {
             return {
               id: item.payload.doc.id,
               ...item.payload.doc.data()
             };

           });
         ....
}     

I am beginner. What changes i need to make to this code so that i get desired output. Thanks


Solution

  • I am assuming you are using Angular Firestore. Try this:

    getEmployees() {
    return this.firestore.collection('employee', ref => ref.orderBy('name','desc')).snapshotChanges(); }