I am developing an application using react native by which user can find fishing boat providers from calendar. the user activity flow is the following;
- Select fishing methods(can be multiple).
- select date from calendar in which number of available fishing boats are shown.
In section number 2, I need to fetch data from firebase realtime database to get data like this.
"2021-01-31": Array [
Object {
"endTime": 1612044000000,
"shipName": "ship nameA",
"startTime": 1612044000000,
},
Object {
"endTime": 1612044000000,
"shipName": "ship nameB",
"startTime": 1612044000000,
},
],
and code is the following;
const getTourSchedules = async () => {
const schedules = {};
await selectedFishingMethods.map((item) => {
const ref = `schedules/${item}`;
Firebase.database().ref(ref).on('value', (snapShot) => {
if (snapShot.exists) {
snapShot.forEach((docs) => {
const tour = docs.val().contents;
const schedule = {
endTime: tour.endTime,
startTime: tour.startTime,
shipName: tour.shipName,
}
if (!schedules[tour.date]) {
schedules[tour.date] = [schedule];
} else {
schedules[tour.date].push(schedule);
}
})
}
})
});
console.log(schedules);
setTours({ ...schedules });
};
The problem is the function above returns empty object and once the page is refreshed, the object is filled up with the data shown above.
Is there any way to wait until the outer map function finishes?
Thank you.
node -V: v14.7.0 expo -V: 3.28.5