Home > front end >  Firestore - Get amount of fields in document
Firestore - Get amount of fields in document

Time:08-03

I have a document with some fields. I want to get the amount of fields.

For Example:

enter image description here

I will get 5.

My Code:

const data = await firestore()
            .collection(groupID)
            .doc('jobs-list')
            .get()
            .then((value) => {
                console.log(value.data.length); // Returns 0
                console.log(value.data()["1"]);
            })

I want to log 5
How do I do this?

CodePudding user response:

There's no direct way in Firestore to get the count of your fields. You can do it by using Object.keys() method as the result you would get in Firestore is an object. See sample code below:

const data = await firestore()
            .collection(groupID)
            .doc('jobs-list')
            .get()
            .then((value) => {
                // This will get the length of the object keys: `5`
                console.log(Object.keys(value.data()).length);
            })
  • Related