Odium
Odium

Reputation: 87

Firestore - Get amount of fields in document

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?

Upvotes: 1

Views: 137

Answers (1)

Marc Anthony B
Marc Anthony B

Reputation: 4069

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);
            })

Upvotes: 1

Related Questions