Search code examples
reactjsfirebasegoogle-cloud-firestorefirebase-admin

Get users orders list of past N days from firestore in reactjs


I want show admin the list of users and there purchases in last N days (say one month). I m storing all the order details in firestore .Here how my database structure looks like image description here . The id you see (i.e : 1612200564594 ) in orders is simply generated using Date().getTime() , which is the time of purchase .

Is there a way so that i can query these orders to show them in a table with the fields given in the image above.

For now i m able to just get the orders list from this code

var db = firebase.firestore();  
    var docRef = db.collection("orders");
    docRef.orderBy("orders", "desc");

    docRef.get().then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
            // i can access fields like address, author Phone no. etc
        });
    });

Solution

  • I'm putting a reference function. You can take and modify it for your needs.

    n_days_ago_from_now = (days) => new Date(new Date().getTime() - 1000*60*60*24*days)
    
    n_days_ago_from_now(3)
    Date Sat Jan 30 2021 15:42:10 GMT+0400    
    n_days_ago_from_now(5)
    Date Thu Jan 28 2021 15:42:13 GMT+0400