简体   繁体   中英

Firestore code to get values of collections

I have a very complex firestore query i would like to get help with, it will be long but i will attach photos for better understanding.

I Would like to execute a firestore query with useEffect that will calculate business income through a week, and eventually sum it to a total monthly income(monthly income is less desired right now).

Photo number 1 of my firebase collection structure: Firebase 集合

Red Circle = This number is representing the month that the appointment is scheduled............................................... Blue Circle = This numbers is representing the dates that the appointment is booked on, if we will combine this two we will get that an appointment is scheduled in October,2nd.

Image number 2:

日期

I will need the query to extract the weekday of the following appointment, meaning if the appointment is at October,2nd the Day will be Friday(5) and create a variable that will hold and sum all the serviceCost prices . By the end of the day i would like to present in graph the income of Sunday, Monday and etc...

This is the start of my useEffect:

  useEffect(() => {
var d = new Date();
var CurrentMonth = d.getMonth() + 1;
firebase
  .firestore()
  .collection("users")
  .doc(uid)
  .collection("appointments")
  .get()
  .then((snapshot) => {
    snapshot.forEach((doc) => {
     //Stuck here
    });
  });

}, []);

Sorry for the long question im just stuck

I think you may have created a problem by creating collections for each month, where you actually treat dates as a continuous stream. What I mean is that back on September 30, your next appointment might have been on October 2, and in your current structure that would require you to query two collections.

I'd recommend storing all appointment dates in a single collection, say appointmentDays and name each document for year-day-month . With that you can then query for the first appointment after (say) September 30 with:

firebase.firestore()
  .collection("appointmentDays")
  .startAfter("2020-09-30")
  .limit(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM