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 }
});