Currently, I am working with Express, React, and MongoDB but I am relatively new to using express and REST API.
My goal is to add an analytics feature to my frontend that focuses on sales and orders. The plan is to allow users to search within a specified date range where each object contains a timestamp that can be compared and collected.
Despite my best efforts, I am encountering issues with my current approach. It appears that the allOrder
variable is not gathering all the objects as expected.
var allOrder = []; // collect all orders and push them
do {
Order.find({ date: sDate })
.then(function(order) {
allOrder.push(order); // pushing order
})
.catch(next);
// create a new search date (usually in another function)
startDate.setDate(startDate.getDate() + 1); // increase by one day
startYear = startDate.getFullYear();
startMonth = startDate.getMonth() + 1;
startDay = startDate.getDate();
if (startMonth < 10) startMonth = "0" + startMonth; // adjust format
sDate = `${startYear}-${startMonth}-${startDay}`; // generate a new search date
dayStart++;
} while (dayStart < days); // verify the day range
res.send(allOrder); // once all dates have been processed, send.
This process effectively increments the day until it reaches a specific date. For instance, I begin by searching for data on 2018-12-25, then progress to the following day, 2018-12-26. While this method works flawlessly during retrieval, I struggle when it comes to storing and sending this information back to the frontend all at once.
Here is the model of my object:
const orderScheme = new Schema({
name: { type: String },
date: { type: String },
customerName: { type: String },
customerPhone: { type: String },
orders: { type: Array }
});