sinevitch
sinevitch

Reputation: 13

Why pagination does not work?

Here's how the document looks in the collection:

const request = {
            carNum: carNum,
            userId: userId,
            comm: comm,
            location: location,
            data: new Date(),
            status: 'wait',
            geo: new firebase.firestore.GeoPoint(latitude, longitude),
        };

When I try to change start point for a query (startAfter). Firebase sends me the same value every time. Why?

let docRef = db.collection('requests').orderBy('data').startAfter(3).limit(3);
        try {
            let doc = await docRef.get()
            console.log(doc);
            console.log('Hello');
        } catch (e) {
            result = e;
        }

Upvotes: 0

Views: 127

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

The starAfter() parameter shall be of similar type than the data you find at this node.

In other words, a query with startAfter(3) will not start after the 3rd item in your collection but after the first document for which data = 3.

You could store your data as a Timestamp:

const request = {
            carNum: carNum,
            userId: userId,
            comm: comm,
            location: location,
            data: new Date().getTime(),   // <- get the Timestamp
            status: 'wait',
            geo: new firebase.firestore.GeoPoint(latitude, longitude),
        };

and query it with a Timestamp:

let docRef = db.collection('requests').orderBy('data').startAfter(1528074000).limit(3)

However, take care that the above is not a DocumentReference but a CollectionReference. You will have to loop over the results you received in order to get the documents.

Upvotes: 1

Related Questions