Although I've managed to find a solution, my main concern is understanding the root cause of the bug. In the scenarios below, MongoDB documents are being created with identical id and date values respectively.
id: {
type: String,
required: true,
unique: true,
default: nanoid(),
immutable: true,
},
dateCreated: {
type: Number,
required: true,
default: Date.now(),
immutable: true,
}
In contrast, in the next examples, each document has a distinct id and creation date.
id: {
type: String,
required: true,
unique: true,
default: () => nanoid(),
immutable: true,
},
dateCreated: {
type: Number,
required: true,
default: () => Date.now(),
immutable: true,
}
I understand that this issue is related to value versus reference in JavaScript, but pinpointing the exact cause remains elusive to me.