简体   繁体   English

Firestore - 获取文档中的字段数量

[英]Firestore - Get amount of fields in document

I have a document with some fields.我有一个包含一些字段的文档。 I want to get the amount of fields.我想获得字段的数量。

For Example:例如:

在此处输入图像描述

I will get 5.我会得到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我要登录5
How do I do this?我该怎么做呢?

There's no direct way in Firestore to get the count of your fields. Firestore 中没有直接的方法来获取您的字段数。 You can do it by using Object.keys() method as the result you would get in Firestore is an object.您可以使用Object.keys()方法来完成,因为您在 Firestore 中获得的结果是 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);
            })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM