最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - firebase firestore get objectarray from document - js - Stack Overflow

programmeradmin8浏览0评论

My firestore database currently looks like this: firestore db

how can I get the value of, say, DEVICES/ID. currently my code returns undefined when i try to get the value.

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. the value of the document ID. doc.data() returns all the fields and its children in what appears to be string format. however when I add DEVICES.ID, it returns undefined. how can i get the nested data as shown in the image?

My firestore database currently looks like this: firestore db

how can I get the value of, say, DEVICES/ID. currently my code returns undefined when i try to get the value.

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. the value of the document ID. doc.data() returns all the fields and its children in what appears to be string format. however when I add DEVICES.ID, it returns undefined. how can i get the nested data as shown in the image?

Share Improve this question asked Jul 7, 2018 at 2:18 Muaaz KaskerMuaaz Kasker 1172 silver badges9 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Your field called DEVICES is actually an array. 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:

doc.data().DEVICES[0].ID
发布评论

评论列表(0)

  1. 暂无评论