6

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?

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

YES! thank you so much. it works. can't believe I didn't see that
is there a way to get all the nodes/children of the devices array
It's just like any other JavaScript array.
Wow you are genius man @DougStevenson!! I was struggling the whole day to extract the values in the array. It worked liked a charm. Thank you very much
@MuaazKasker thanks for posting such a valuable question in this forum :) :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.