How to fetch values from Firestore with where() followed by startAt() and endAt()

I am using anguarfire2 and I just need to fetch values from Firestore which is from specific type of customers and order by positions

I have tried the below but not giving me any result

angularFirestore.collection('customers', ref => ref.where('type', '==', type)
            .orderBy('position')
            .startAt(1)
            .endAt(2))

But

   angularFirestore.collection('customers', ref => ref.orderBy('position')
            .startAt(1)
            .endAt(2) )

it works. It provides me results regardless of customer type.

Is that possible to fetch values with combination of where, orderBy, startAt and endAt?

1 answer

  • answered 2019-06-14 10:56 shikhar

    You need t paginate the queries as referred in the document here enter link description here

    You can also refer the code below to achieve the same:

    let first = db.collection('cities')
               .orderBy('population')
               .limit(3);
    
    let paginate = first.get()
    .then((snapshot) => {
    // ...
    
    // Get the last document
    let last = snapshot.docs[snapshot.docs.length - 1];
    
    // Construct a new query starting at this document.
    // Note: this will not have the desired effect if multiple
    // cities have the exact same population value.
    let next = db.collection('cities')
      .orderBy('population')
      .startAfter(last.data().population)
      .limit(3);
    
    // Use the query for pagination
    // ...
    

    });