How to get data from an array of specific paths in Firebase?
I'm working with dates now, and I structured the database like this:
database[userId][work][year][month][date]
So if I want to get the working hours of an user, on a specific date, I call:
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
If I need just one date's data, this function works perfectly. But I want to show the user a whole week of data. Here comes the problem.
I have two possible solutions, but neither of them are practical.
//loop through an array of the week's dates
const weeksDates = [
{year: 2018, month: 11, date: 12},
{year: 2018, month: 11, date: 13},
{year: 2018, month: 11, date: 14},
{year: 2018, month: 11, date: 15},
{year: 2018, month: 11, date: 16},
{year: 2018, month: 11, date: 17},
{year: 2018, month: 11, date: 18},
];
const userId = "userId";
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
//loop through the dates, and fetch data at each date
const getWorkDataOfWeek = (userId, weeksDates) => {
const data = ;
const pushToData = value => data.push(value)
weeksDates.forEach(dateObj => {
const { year, month, date } = dateObj;
getWorkData(userId, year, month, date, pushToData)
})
}
//fetch the whole database, and filter it
const getWorkDataOfWeek = (userId, weeksDates, callback) => {
usersRef.child(userId).on("value", snap => {
const workData = snap.val();
const data = weeksDates.map(dateObj => {
const { year, month, date } = dateObj;
return workData[year][month][date];
})
callback(data)
})
}The problem with the first method is that it creates too much requests. The problem with the second is that it requests too large amount of data. Is there a solution for firebase which is similar to the first except that it just requests once?
javascript firebase firebase-realtime-database
add a comment |
I'm working with dates now, and I structured the database like this:
database[userId][work][year][month][date]
So if I want to get the working hours of an user, on a specific date, I call:
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
If I need just one date's data, this function works perfectly. But I want to show the user a whole week of data. Here comes the problem.
I have two possible solutions, but neither of them are practical.
//loop through an array of the week's dates
const weeksDates = [
{year: 2018, month: 11, date: 12},
{year: 2018, month: 11, date: 13},
{year: 2018, month: 11, date: 14},
{year: 2018, month: 11, date: 15},
{year: 2018, month: 11, date: 16},
{year: 2018, month: 11, date: 17},
{year: 2018, month: 11, date: 18},
];
const userId = "userId";
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
//loop through the dates, and fetch data at each date
const getWorkDataOfWeek = (userId, weeksDates) => {
const data = ;
const pushToData = value => data.push(value)
weeksDates.forEach(dateObj => {
const { year, month, date } = dateObj;
getWorkData(userId, year, month, date, pushToData)
})
}
//fetch the whole database, and filter it
const getWorkDataOfWeek = (userId, weeksDates, callback) => {
usersRef.child(userId).on("value", snap => {
const workData = snap.val();
const data = weeksDates.map(dateObj => {
const { year, month, date } = dateObj;
return workData[year][month][date];
})
callback(data)
})
}The problem with the first method is that it creates too much requests. The problem with the second is that it requests too large amount of data. Is there a solution for firebase which is similar to the first except that it just requests once?
javascript firebase firebase-realtime-database
add a comment |
I'm working with dates now, and I structured the database like this:
database[userId][work][year][month][date]
So if I want to get the working hours of an user, on a specific date, I call:
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
If I need just one date's data, this function works perfectly. But I want to show the user a whole week of data. Here comes the problem.
I have two possible solutions, but neither of them are practical.
//loop through an array of the week's dates
const weeksDates = [
{year: 2018, month: 11, date: 12},
{year: 2018, month: 11, date: 13},
{year: 2018, month: 11, date: 14},
{year: 2018, month: 11, date: 15},
{year: 2018, month: 11, date: 16},
{year: 2018, month: 11, date: 17},
{year: 2018, month: 11, date: 18},
];
const userId = "userId";
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
//loop through the dates, and fetch data at each date
const getWorkDataOfWeek = (userId, weeksDates) => {
const data = ;
const pushToData = value => data.push(value)
weeksDates.forEach(dateObj => {
const { year, month, date } = dateObj;
getWorkData(userId, year, month, date, pushToData)
})
}
//fetch the whole database, and filter it
const getWorkDataOfWeek = (userId, weeksDates, callback) => {
usersRef.child(userId).on("value", snap => {
const workData = snap.val();
const data = weeksDates.map(dateObj => {
const { year, month, date } = dateObj;
return workData[year][month][date];
})
callback(data)
})
}The problem with the first method is that it creates too much requests. The problem with the second is that it requests too large amount of data. Is there a solution for firebase which is similar to the first except that it just requests once?
javascript firebase firebase-realtime-database
I'm working with dates now, and I structured the database like this:
database[userId][work][year][month][date]
So if I want to get the working hours of an user, on a specific date, I call:
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
If I need just one date's data, this function works perfectly. But I want to show the user a whole week of data. Here comes the problem.
I have two possible solutions, but neither of them are practical.
//loop through an array of the week's dates
const weeksDates = [
{year: 2018, month: 11, date: 12},
{year: 2018, month: 11, date: 13},
{year: 2018, month: 11, date: 14},
{year: 2018, month: 11, date: 15},
{year: 2018, month: 11, date: 16},
{year: 2018, month: 11, date: 17},
{year: 2018, month: 11, date: 18},
];
const userId = "userId";
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
//loop through the dates, and fetch data at each date
const getWorkDataOfWeek = (userId, weeksDates) => {
const data = ;
const pushToData = value => data.push(value)
weeksDates.forEach(dateObj => {
const { year, month, date } = dateObj;
getWorkData(userId, year, month, date, pushToData)
})
}
//fetch the whole database, and filter it
const getWorkDataOfWeek = (userId, weeksDates, callback) => {
usersRef.child(userId).on("value", snap => {
const workData = snap.val();
const data = weeksDates.map(dateObj => {
const { year, month, date } = dateObj;
return workData[year][month][date];
})
callback(data)
})
}The problem with the first method is that it creates too much requests. The problem with the second is that it requests too large amount of data. Is there a solution for firebase which is similar to the first except that it just requests once?
//loop through an array of the week's dates
const weeksDates = [
{year: 2018, month: 11, date: 12},
{year: 2018, month: 11, date: 13},
{year: 2018, month: 11, date: 14},
{year: 2018, month: 11, date: 15},
{year: 2018, month: 11, date: 16},
{year: 2018, month: 11, date: 17},
{year: 2018, month: 11, date: 18},
];
const userId = "userId";
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
//loop through the dates, and fetch data at each date
const getWorkDataOfWeek = (userId, weeksDates) => {
const data = ;
const pushToData = value => data.push(value)
weeksDates.forEach(dateObj => {
const { year, month, date } = dateObj;
getWorkData(userId, year, month, date, pushToData)
})
}
//fetch the whole database, and filter it
const getWorkDataOfWeek = (userId, weeksDates, callback) => {
usersRef.child(userId).on("value", snap => {
const workData = snap.val();
const data = weeksDates.map(dateObj => {
const { year, month, date } = dateObj;
return workData[year][month][date];
})
callback(data)
})
}//loop through an array of the week's dates
const weeksDates = [
{year: 2018, month: 11, date: 12},
{year: 2018, month: 11, date: 13},
{year: 2018, month: 11, date: 14},
{year: 2018, month: 11, date: 15},
{year: 2018, month: 11, date: 16},
{year: 2018, month: 11, date: 17},
{year: 2018, month: 11, date: 18},
];
const userId = "userId";
const getWorkData = (userId, year, month, date, callback) => {
usersRef.child(`${userId}/work/${year}/${month}/${date}`).on("value", snap =>
callback(snap.val())
)
}
//loop through the dates, and fetch data at each date
const getWorkDataOfWeek = (userId, weeksDates) => {
const data = ;
const pushToData = value => data.push(value)
weeksDates.forEach(dateObj => {
const { year, month, date } = dateObj;
getWorkData(userId, year, month, date, pushToData)
})
}
//fetch the whole database, and filter it
const getWorkDataOfWeek = (userId, weeksDates, callback) => {
usersRef.child(userId).on("value", snap => {
const workData = snap.val();
const data = weeksDates.map(dateObj => {
const { year, month, date } = dateObj;
return workData[year][month][date];
})
callback(data)
})
}javascript firebase firebase-realtime-database
javascript firebase firebase-realtime-database
edited Nov 17 '18 at 14:56
Frank van Puffelen
233k29380407
233k29380407
asked Nov 17 '18 at 12:29
Gergő HorváthGergő Horváth
501112
501112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Principle of an object database like Firebase is to be able to arrange the data the way you want and will need it for futur use. This prevent heavy CPU loads when retrieving data and so on, you achieve this by doing denormalization. This meanss you anticipate how you would need the data back and write it has so. So you need data by week and by date, write the data has week and date in the first place.
Something like this:
/UID/year/month/date
/UID/year/week/date . ( you should already know that week is 12, 13 ...)
With Firebase it's really easy to save data on multiple path at once, you use multi paths updates.
Articles about denormalization: https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html
Articles about multi path updates: https://medium.com/@danbroadbent/firebase-multi-path-updates-updating-denormalized-data-in-multiple-locations-b433565fd8a5
P.S. I would also add that it seems to me that you want to read that data only when you need it so use once instead of on.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351282%2fhow-to-get-data-from-an-array-of-specific-paths-in-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Principle of an object database like Firebase is to be able to arrange the data the way you want and will need it for futur use. This prevent heavy CPU loads when retrieving data and so on, you achieve this by doing denormalization. This meanss you anticipate how you would need the data back and write it has so. So you need data by week and by date, write the data has week and date in the first place.
Something like this:
/UID/year/month/date
/UID/year/week/date . ( you should already know that week is 12, 13 ...)
With Firebase it's really easy to save data on multiple path at once, you use multi paths updates.
Articles about denormalization: https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html
Articles about multi path updates: https://medium.com/@danbroadbent/firebase-multi-path-updates-updating-denormalized-data-in-multiple-locations-b433565fd8a5
P.S. I would also add that it seems to me that you want to read that data only when you need it so use once instead of on.
add a comment |
Principle of an object database like Firebase is to be able to arrange the data the way you want and will need it for futur use. This prevent heavy CPU loads when retrieving data and so on, you achieve this by doing denormalization. This meanss you anticipate how you would need the data back and write it has so. So you need data by week and by date, write the data has week and date in the first place.
Something like this:
/UID/year/month/date
/UID/year/week/date . ( you should already know that week is 12, 13 ...)
With Firebase it's really easy to save data on multiple path at once, you use multi paths updates.
Articles about denormalization: https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html
Articles about multi path updates: https://medium.com/@danbroadbent/firebase-multi-path-updates-updating-denormalized-data-in-multiple-locations-b433565fd8a5
P.S. I would also add that it seems to me that you want to read that data only when you need it so use once instead of on.
add a comment |
Principle of an object database like Firebase is to be able to arrange the data the way you want and will need it for futur use. This prevent heavy CPU loads when retrieving data and so on, you achieve this by doing denormalization. This meanss you anticipate how you would need the data back and write it has so. So you need data by week and by date, write the data has week and date in the first place.
Something like this:
/UID/year/month/date
/UID/year/week/date . ( you should already know that week is 12, 13 ...)
With Firebase it's really easy to save data on multiple path at once, you use multi paths updates.
Articles about denormalization: https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html
Articles about multi path updates: https://medium.com/@danbroadbent/firebase-multi-path-updates-updating-denormalized-data-in-multiple-locations-b433565fd8a5
P.S. I would also add that it seems to me that you want to read that data only when you need it so use once instead of on.
Principle of an object database like Firebase is to be able to arrange the data the way you want and will need it for futur use. This prevent heavy CPU loads when retrieving data and so on, you achieve this by doing denormalization. This meanss you anticipate how you would need the data back and write it has so. So you need data by week and by date, write the data has week and date in the first place.
Something like this:
/UID/year/month/date
/UID/year/week/date . ( you should already know that week is 12, 13 ...)
With Firebase it's really easy to save data on multiple path at once, you use multi paths updates.
Articles about denormalization: https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html
Articles about multi path updates: https://medium.com/@danbroadbent/firebase-multi-path-updates-updating-denormalized-data-in-multiple-locations-b433565fd8a5
P.S. I would also add that it seems to me that you want to read that data only when you need it so use once instead of on.
answered Nov 17 '18 at 14:26
Simon CadieuxSimon Cadieux
2049
2049
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351282%2fhow-to-get-data-from-an-array-of-specific-paths-in-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown