繁体   English   中英

Firestore如何从firestore文件中获取密钥

[英]Firestore how to get keys from firestore document

使用下面的查询,我可以获取文档和doc.data()。name来获取键'name'的值。 我想获得该文档中的所有密钥。 我怎么能得到它?

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());

//**I want to get all keys in the document here.**

    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

基于我在这个答案中的例子,你可以做到:

docRef.get().then(function(doc) {
    if (doc.exists) {
        let json = doc.data();
        console.log("Document data:", json);
        console.log("Document keys:", Object.keys(json));
        Object.keys(json).forEach((name) => {
          console.log(name, json[name]);
        });
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

暂无
暂无

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

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