0

I'm on a vuejs and firebase project. And I would like to display only the data recorded today. But I don't know how to do it. Can you please help me.


   created() {
    const dfRef = db.ref("Colis Requetes")
      dfRef.orderByChild("created_at").on("value", (dataSnapshot) => {
        const itemsArray = [];
        dataSnapshot.forEach((childSnapshot) => {
          const childData = childSnapshot.val();
          const childDataKey = childSnapshot.key;
          this.Colis = childData;
          itemsArray.push({
            id: childDataKey,
            client_name: childData.client_name,
            client_phone: childData.client_phone,
            colis_type: childData.colis_type,
            payment_method: childData.payment_method,
          });
        });
        this.Colis = itemsArray.reverse();
      });
   }

The orderByChild property is for displaying data from the most recent date to the oldest date. However, I want only the recordings made today to appear on the page.

I'm on a vuejs and firebase project. And I would like to display only the data recorded today. But I don't know how to do it. Can you please help me.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

You'll want to use a filtering operation for that, most likely startAt.

If your created_at contains a timestamp (going forward, please include such information in your question), that'd be something like:

var start = new Date();
start.setUTCHours(0,0,0,0); // based on https://stackoverflow.com/a/8636674

const dfRef = db.ref("Colis Requetes")
dfRef
  .orderByChild("created_at")
  .startAt(start.getTime()) // 
  .on("value", (dataSnapshot) => {
    ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807