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.
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.
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.
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 .
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().
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With