13

Get data ordered by document Id in Firestore by Javascript

In Andorid, I can use the query go to specific documents

mQuery = docRef.whereEqualTo("name", name)
                .whereEqualTo("valid",true)
                .orderBy(FieldPath.documentId())
                .startAt(lastDocId)
                .limit(LIMIT);

It works, but in Javascript, I try to copy a similar query, it didn't work.

var db = firebase.firestore();
    var goQuery = docRef.where("name", "==", name)
                        .where("valid", "==", true)
                        .orderBy(db.FieldPath.documentId())
                        .startAfter("id0070")
                        .limit(LIMIT);

It give a error:

Uncaught TypeError: Cannot read property 'documentId' of undefined at HTMLButtonElement.document.getElementById.onclick

Any idea? Thank you for your help.

Jack Wilson
  • 6,065
  • 12
  • 29
  • 52
  • 1
    why is one `FieldPath.documentId()` and other is `db.FieldPath.documentId()` ? – epascarello Oct 12 '18 at 04:14
  • Reply to epascarello: cause FieldPath.documentId() will give error:Uncaught ReferenceError: FieldPath is not defined – Jack Wilson Oct 12 '18 at 04:17
  • @MikeLin I don't think you get what he means. It's simply why is the first one referred as **db.FieldPath** and the other one is **FieldPath**. – choz Oct 12 '18 at 04:19

1 Answers1

25

By trial and error, the correct grammar is

var goQuery = docRef.where("name", "==", name)
                    .where("valid", "==", true)
                    .orderBy(firebase.firestore.FieldPath.documentId())
                    .startAfter("id0070")
                    .limit(LIMIT);

firebase.firestore.FieldPath.documentId()

Jack Wilson
  • 6,065
  • 12
  • 29
  • 52