79413892

Date: 2025-02-05 07:06:00
Score: 0.5
Natty:
Report link

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},
    },
  ],
});
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When the
  • Low reputation (1):
Posted by: Karandeep Singh