Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data ordered by document Id in Firestore by Javascript?

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.

like image 340
Jack Wilson Avatar asked Oct 12 '18 04:10

Jack Wilson


People also ask

How do I get data from firestore using ID?

How do I get data from firestore using ID? getFirestore() → Firestore Database. doc() → It takes references of database, collection name and ID of a document as arguments. getDoc() → getDoc() query gets data of a specific document from collection based on references mentioned in the doc() method.

How do I get sorted data from firestore?

By default, a query retrieves all documents that satisfy the query in ascending order by document ID. You can specify the sort order for your data using orderBy() , and you can limit the number of documents retrieved using limit() . Note: An orderBy() clause also filters for existence of the given field.

How do I get a random document ID in firestore?

An easy way to grab random documents is get all the posts keys into an array ( docA , docB , docC , docD ) then shuffle the array and grab the first three entries, so then the shuffle might return something like docB , docD , docA .

How do you get a collection inside a document in firestore?

We use the data parameter to get docPath , the value of the Firestore document path (slash-separated). This value is passed from the client calling the Cloud Function (see below). We then call the asynchronous listCollections() method on the DocumentReference created by using docPath (i.e. admin. firestore().


1 Answers

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()

like image 95
Jack Wilson Avatar answered Oct 08 '22 18:10

Jack Wilson