0

I'm using express, react and MongoDB. I'm totally new to express and REST API.

I'm trying to implement an analytics portion for my front end on sales, orders etc. The idea is to allow a range of dates to be searched, where each object has a date stamp in which I then compare and gather.

My approach is not working, allOrder doesn't seem to gather all the object as I anticipated.

var allOrder = []; // gather all orders and push them

do {
  Order.find({ date: sDate })
    .then(function(order) {
      allOrder.push(order); //pushing order
    })
    .catch(next);

  // create new search date (normally in another function)
  startDate.setDate(startDate.getDate() + 1); // increment a day
  startYear = startDate.getFullYear();
  startMonth = startDate.getMonth() + 1;
  startDay = startDate.getDate();
  if (startMonth < 10) startMonth = "0" + startMonth; // fix format

  sDate = `${startYear}-${startMonth}-${startDay}`; // create new search date
  dayStart++;
} while (dayStart < days); // check the day range

res.send(allOrder); //once all days are done, send.

This works in the sense that it increases the day until I reach a certain date. For example, I find for 2018-12-25, then increase a day, and find the next day 2018-12-26. It all works except for when it comes to ending the data back to the front. On each find I do get the all the data, I just can't seem to store it THEN send it all together to the front.

my object model

const orderScheme = new Schema({
  name: { type: String },
  date: { type: String },
  customerName: { type: String },
  customerPhone: { type: String },
  orders: { type: Array }
});
Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31
Robolisk
  • 1,682
  • 5
  • 22
  • 49
  • `Order.find()` is asynchronous; this code will call `res.send(allOrder);` *before* even the first DB operation has finished. I'll also go out on a limb and guess that mongodb has support for grabbing records inbetween timestamps. Composing dates like that is not only unreadable and hard to debug but not necessary at all. –  Jan 15 '19 at 18:59
  • explains why my front end was acting funny. I'm not sure how to go about this then. Would I just have my front end constantly receive data and add it to state until I receive a 'end of data' flag? – Robolisk Jan 15 '19 at 19:00
  • 1
    No, if you do have to make multiple queries, you're supposed to use `Promise.all()`. However there's this: https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb and this: http://www.tinnedgeek.com/how-to-search-a-mongodb-database-for-entries-between-two-dates-or-times/ –  Jan 15 '19 at 19:02
  • @ChrisG I wasn't aware those parameters were options. It worked. – Robolisk Jan 15 '19 at 19:16

1 Answers1

0

as Chris helped me inform me in my comments, the solution was very simple.

Order.find ({
    date: {
        $gte : sDate,
        $lt : eDate
    }
}).then (function (order){
    res.send(order);
}).catch(next)

my sDate is a my own timestamp (or any value you want I guess) and likewise for eDate. This works flawlessly and elegantly.

Robolisk
  • 1,682
  • 5
  • 22
  • 49