const nft = new mongoose.Schema(
{
name: String,
owner: String,
ownerPicture: String,
group: String,
groupPicture: String,
code: {
type: String,
default: uuidv4(),
unique:true
},
producedBy: String,
competeIn: String,
description: String,
nft: String,
nftDestination: String,
},
{ timestamps: true }
);
This is my mongoose Schema and it working fine. But the the default value for code is giving me the same uuid for every model. I want it to be unique every time. When ever I make a new model of this schema. The code value always remain the same. Please give me a solution to this.
CodePudding user response:
The Express execute code from top to bottom. That means it will go through your code and execute uuidv4()
that will generate unique string and Express will assume that is the default value for all documents.
In other words, uuidv4()
will be executed only once, and not on each document generation.
I didn't test this, but you can try this:
code: {
type: String,
default: () => uuidv4(),
unique: true
},