I want to try and get a whole ref from a Firebase Realtime Database into an object, while changing some of its properties:
const [events, setEvents] = React.useState([]);
get(child(ref(getDatabase()), `events`)).then(snap => {
setEvents([snap.val()])
})
// creating my object:
events.map(ev => Object.keys(ev).map(item => ({
title: ev[item].txt, // the original proparty from firebase is "txt", I wabt to get it as "title"
image: ev[item].imgUrl,
description: ev[item].description
})))
It returned this:
With an extra "0" parent, but how can I get it this way:
This is the json:
"events" : {
"-MmZ5gjaPgQZj58NX3Us" : {
"imgFile" : "77mhw.jpg",
"imgUrl" : "https://firebasestorage.googleapis.com/v0/b/zevik-4a379.appspot.com/o/77mhw.jpg?alt=media&token=e530272c-6e36-409d-ac68-0042ac17b971",
"txt" : "sometxt"
},
"-MmZ5grbFb-6Bj5ew7Gv" : {
"imgFile" : "bptmli.jpg",
"imgUrl" : "https://firebasestorage.googleapis.com/v0/b/zevik-4a379.appspot.com/o/bptmli.jpg?alt=media&token=5f99b26c-e184-43af-bc8b-95484b94f6a5",
"txt" : "sometxt"
},
"-MmZ5gxeoHGTUDZ328RE" : {
"imgFile" : "xj5fi.jpg",
"imgUrl" : "https://firebasestorage.googleapis.com/v0/b/zevik-4a379.appspot.com/o/xj5fi.jpg?alt=media&token=7058ea83-1abb-47f7-bcf3-eae2fb2af679",
"txt" : "sometxt"
}
}
Thanks!
I think the extra 0 level comes from the fact that you use [] in this call:
setEvents([snap.val()])
As far as I can tell that should be:
setEvents(snap.val())