简体   繁体   中英

How to get all the keys from a specific doc of a collection in firestore?

I have already learned firestore CRUD. It is easy to get value from a document if fields are known of the doc. My question is how to grab the list of keys that the specific doc has. I need it because I want to show the fields and values in a table. If I add more fields in future in the same doc then the table will automatically be updated with the new fields and values.

在此处输入图片说明

Thanks.

As mentioned in the docs you can get a document snapshot using get() method of a document reference and then use data() method of snapshot to get all the data in the document as an object.

https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document

var docRef = db.collection("users").doc("alovelace");

docRef.get().then(function(doc) {
    if (doc.exists) {
        // Here you can get your data
        console.log("Document data:", doc.data());
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
});

Then you have an object of key values representing your document data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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