I am trying to assign a role property to users and have declared that in the schema but mongodb is ignoring it, if I define a default it will only set the default value in DB and not the one defined in the request.and if I remove the default value it will not set "role" at all.
Usermodel ⬇
const userSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "Name is required"],
},
email: {
type: String,
required: [true, "Email is required"],
unique: true,
lowercase: true,
validate: [validator.isEmail, "Please provide a valid email"],
},
photo: String,
role: {
type: String,
enum: ["user", "moderator", "admin"],
default: "user",
},
password: {
type: String,
required: [true, "Provide a password, at least 8 characters"],
minlength: 8,
select: false, // to exlude it from respond data.
},
confirmPassword: {
type: String,
required: [true, "Please confirm your password"],
validate: {
validator: function (cnfmPass) {
return cnfmPass === this.password;
},
message: "Passwords do not match",
},
},
My post Request
{
"name": "Test user",
"email": "test@user.com",
"role": "admin",
"password": "pass1234",
"confirmPassword": "pass1234"
}
the result ⬇
_id: 63245a920714153bfc66b6c3
role: **"user"**
name: "Test user"
email: "test@user.com"
password: "$2a$12$8W3VvkZBRhUGjEgUCN9iducr0VLntyEPCweeoqJ53ToMzGSZy29pi"
__v: 0