简体   繁体   English

firebase firestore从document-js获取对象/数组

[英]firebase firestore get object/array from document - js

My firestore database currently looks like this: firestore db 我的firestore数据库目前看起来像这样: firestore db

how can I get the value of, say, DEVICES/ID. 我怎样才能获得DEVICES / ID的价值。 currently my code returns undefined when i try to get the value. 当我尝试获取值时,我的代码返回undefined。

var userDeviceRef = db.collection("USERS").doc(data.uid);
userDeviceRef.get().then(function(doc){
    if (doc.exists) {
        console.log("Document data:", doc.data());
        console.log("document customdata foo: " + doc.data().DEVICES.ID);
    }
}

data.uid returns a proper value. data.uid返回适当的值。 the value of the document ID. 文档ID的值。 doc.data() returns all the fields and its children in what appears to be string format. doc.data()以字符串格式返回所有字段及其子doc.data() however when I add DEVICES.ID , it returns undefined. 但是当我添加DEVICES.ID ,它返回undefined。 how can i get the nested data as shown in the image? 如何获得图像中显示的嵌套数据?

Your field called DEVICES is actually an array. 你的名为DEVICES字段实际上是一个数组。 As far as I can tell, it has at least one element in it. 据我所知,它至少有一个元素。 If you want the value of the ID field of the first element of that array, you'll have to index into that array: 如果你想要该数组的第一个元素的ID字段的值,你将必须索引到该数组:

doc.data().DEVICES[0].ID

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

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