When using the Mongoose package to retrieve data from MongoDB and send it to a browser, I encountered an issue with reassigning a date object to a string. Despite my attempts to change the date object to a string before passing it to the HTML file, it did not get reassigned. I wonder what could be causing this?
I experimented with reassigning it to a different Date() instance, which worked fine. Are there specific schema rules that still apply when accessing or modifying data in a callback function? How can I successfully convert this variable into a string that represents a date?
function index(req, res, next) {
Flight.find({},function(err, flights){
flights.forEach(function(flight){
flight.departs = 'any string';
console.log(flight.departs);
})
res.render('flights/index', {flightDocs: flights, title: 'Flights', moment});
})
}
The console displays the original date objects retrieved from the database:
2020-11-17T23:56:00.000Z
2019-11-17T22:01:00.000Z
2019-11-17T23:05:00.000Z
2019-11-17T22:03:00.000Z
2020-11-18T00:01:00.000Z
2020-11-18T00:01:00.000Z