簡體   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