Beginner of Node.js and MongoDB. I just wondering why there is no "isAdmin" in my MongoDB and how to fix it? Thank you~
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema(
{
username: {
type: String,
required: true,
unique: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
required: false,
},
},
{ timestamps: true }
);
export default mongoose.model("User", UserSchema);
The user at database is without "isAdmin" like this:
username: "johnAdmin"
email: "[email protected]"
password: "$2a$10$pjuVsWEe8BhP0nvgNcjS3eSp0vKwyzDUZNGDf5nYn3/Rn2yunh.iy"
createdAt: 2022-11-05T21:44:42.402 00:00
updatedAt: 2022-11-05T21:44:42.402 00:00
I have tried to change "required"
to true
, but it still does not work.
CodePudding user response:
You can set a default value for isAdmin
isAdmin: {
type: Boolean,
default: false,
},
or
check while saving the user you have isAdmin field in the object
let user = User({
username: req.body.username,
email: req.body.email,
password: req.body.password,
isAdmin: req.body.isAdmin
})
user.save()