0

Using AngularFire, let's say I want to orderby 'year', and then by 'brand'. It can be done in Firebase by:

Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);

But I am not seeing a way to do so using AngularFire's orderBy. I would expect something like this to work but it throws an error ERROR TypeError: Cannot read properties of undefined (reading 'length'):

...
this.db
        .collection('myCollection', (ref) => ref.orderBy('year').orderBy('brand'))
        .snapshotChanges()
...

Is it possible to order by one thing, then another, using AngularFire?

EDIT: Here's the full method:

fetchBaseballCards(uid: any) {
    this.uiService.loadingStateChanged.next(true);
    this.fbSubs.push(
      this.db
        .collection(uid, (ref) => ref.orderBy('year').orderBy('brand'))
        .snapshotChanges()
        .pipe(
          map((docArray) => {
            return docArray.map((doc: any) => {
              return {
                id: doc.payload.doc.id,
                brand: doc.payload.doc.data().brand,
                buyDate: doc.payload.doc.data().buyDate,
                buyPrice: doc.payload.doc.data().buyPrice,
                cardNumber: doc.payload.doc.data().cardNumber,
                firstName: doc.payload.doc.data().firstName,
                grade: doc.payload.doc.data().grade,
                inventoryId: doc.payload.doc.data().inventoryId,
                lastName: doc.payload.doc.data().lastName,
                sellingPrice: doc.payload.doc.data().sellingPrice,
                soldDate: doc.payload.doc.data().soldDate,
                soldPrice: doc.payload.doc.data().soldPrice,
                year: doc.payload.doc.data().year,
              };
            });
          })
        )
        .subscribe(
          (cards: any) => {
            this.uiService.loadingStateChanged.next(false);
            this.baseballCards = cards;
            this.baseballCardsChanged.next([...this.baseballCards]);
          },
          (err) => {
            this.uiService.loadingStateChanged.next(false);
            this.uiService.showSnackbar('Fetching Cards failed, please try again later', undefined, 3000);
            this.baseballCardsChanged.next(undefined);
          }
        )
    );
  }

and full error message:

ERROR TypeError: Cannot read properties of undefined (reading 'length')
    at MatTableDataSource._filterData (table.js:724)
    at MapSubscriber.project (table.js:701)
    at MapSubscriber._next (map.js:29)
    at MapSubscriber.next (Subscriber.js:49)
    at CombineLatestSubscriber.notifyNext (combineLatest.js:73)
    at InnerSubscriber._next (InnerSubscriber.js:11)
    at InnerSubscriber.next (Subscriber.js:49)
    at BehaviorSubject.next (Subject.js:39)
    at BehaviorSubject.next (BehaviorSubject.js:30)
    at MatTableDataSource.set data [as data] (table.js:635)
Khepf
  • 352
  • 1
  • 4
  • 24
  • 1
    Yes, AngularFire supports multiple `orderBy` query. For that you have to create index in firestore. The error you are getting is pointing that somewhere, the query is giving undefined. Can you share the entire code? – Prabir Nov 23 '21 at 13:10
  • I added the full method above. – Khepf Nov 23 '21 at 15:58
  • Do you know from which line of the code the error is coming? If not, can you narrow down the error by adding console.log() statements? Also what is there in the Firestore database? – Prabir Nov 24 '21 at 10:06

0 Answers0