Reputation: 99
Hi I am trying to access nested data in firebase real time database with a cloud functions, my data looks like:
{
"RANGE1": {
"left": 2,
"overlayImage": "",
"right": 1,
"submittedInfo": {
"left_1": {
"TimeStamp": "2020-02-16 19:05:35",
"horozontalAccuracy": 10,
"latitude": 0000,
"longitude": 0000
},
"left_2": {
"TimeStamp": "2020-02-16 19:23:59",
"horozontalAccuracy": 10,
"latitude": 0000,
"longitude": 0000
},
"left_3": {
"TimeStamp": "2020-02-20 19:35:28",
"horozontalAccuracy": 10,
"latitude": 0000,
"longitude": 0000
},
"right_1": {
"TimeStamp": "2020-02-16 19:08:30",
"horozontalAccuracy": 10,
"latitude": 0000,
"longitude": 0000
}
}
},
"RANGE2": {
"left": 1,
"overlayImage": "",
"right": 1,
"submittedInfo": {
"left_1": {
"TimeStamp": "2020-02-12 22:07:11",
"horozontalAccuracy": 5,
"latitude": 0000,
"longitude": 0000
},
"right_1": {
"TimeStamp": "2020-02-12 22:36:36",
"horozontalAccuracy": 11.721012922544011,
"latitude": 0000,
"longitude": 0000
}
}
}
}
and I have a function that is supposed to filter and flatten this data. But I can't seem to figure out how to get deep in to the nodes. everything I try I seem to get stuck in accessing [object Object] or the typeof() returns a string when it should be a JSON object still
admin.database().ref(".../range").once('value').then((snapshot) => {
var flagInfo = [];
snapshot.forEach((range) => {
console.log("range is : " + range) // [object Object]
console.log("range[submitted] " + range['submittedInfo']) // undefined???
// what I need to do is something like:
range["submittedInfo"].forEach((flagData) => {
console.log(flagData.key)
console.log(flagData["latitude"]) // and so on for the rest of the keys
}) // end
})
Upvotes: 0
Views: 74
Reputation: 4272
Please take a look on this reference. There is quite good example in forEach part.
You should use methods child()
and val()
. I do not have testing playground at hand at the moment, but it should be something like range.val()
to get range and range.child('submittedInfo').val()
to get 'submittedInfo' data, but you have to figure it out how to do it exactly. If you will not manage let me know, I will try to implement it on my side.
I hope it will help!
Upvotes: 1