When the file is compiled on deployment, new Date()
gets also invoked which returns a time stamp of that moment when the server was started, thus here on default value looks like text: { type: String, default: "2025-02-05T06:54:52.374Z" }
which is static.
now we need to make it dynamic for that we have to figure out a way to invoke new Date(), whenever something is saved this can be achieved by either of the two ways below.
const reportUsSchema = new mongoose.Schema({
texts: [
{
text: { type: String, default: () => { return new Date() } },
date_time: { type: Date, default: new Date() },
},
],
});
//OR
const reportUsSchema = new mongoose.Schema({
texts: [
{
text: { type: String, default: Date.now},
},
],
});