//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)
})
}