Question: how can I take only the week numbers (without information what is inside each week)?
I did like this, but I also get what's inside every week. I only need week numbers:
const db = firebase.database();
const weeksRef = db.ref('MyApp/Weeks');
weeksRef.on('value', items =>{
return items.val()
})
Firebase always loads complete nodes from the Realtime Database. There is no way to get the keys from your JSON, without also getting their values.
The closest you can get with your current data structure is to only use the keys of the JSON:
const db = firebase.database();
const weeksRef = db.ref('MyApp/Weeks');
weeksRef.on('value', items =>{
items.forEach((child) => {
console.log(child.key);
})
})
The above will print only the keys, but it is still downloading all data under each key too. To prevent that, you will need to modify your data structure to allow the use-case, for example by adding a node that keeps only the week numbers:
WeekNumbers: {
"1": true,
"2": true,
"3": true,
"4": true,
"5": true,
"6": true
}
The true
values here are needed, since Firebase won't store a key unless there is a value under it.
With the above structure you can get the same output as before with:
const db = firebase.database();
const weeksRef = db.ref('MyApp/WeekNumbers');
weeksRef.on('value', items =>{
items.forEach((child) => {
console.log(child.key);
})
})
As you can see, the code remains almost the same, but a lot less data is being downloaded.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments