Reputation: 134
I have a mongoose model and I want to pass my request body data to it and then with create function add it to my database, but it doesn't work and it doesn't give me any errors and it just stock in request there is my model
const usersSchema = new mongoose.Schema({
firstname: {
type: String,
required: [true, "first name is required"],
trim: true,
minlength: {
value: 2,
message: "your first name could not be less than 2 characters!",
},
maxlength: {
values: 25,
message: "your first name could not be more than 25 characters!",
},
},
lastname: {
type: String,
required: [true, "last name is required"],
trim: true,
minlength: {
values: 2,
message: "your last name could not be less than 2 characters!",
},
maxlength: {
values: 35,
message: "your last name could not be more than 35 characters!",
},
},
username: {
type: String,
unique: [true, "username has already been used!"],
required: [true, "username is required!"],
lowercase: true,
trim: true,
minlength: {
values: 6,
message: "your username must be at least 6 characters",
},
maxlength: {
values: 35,
message: "your user name could not be bigger than 35 characters",
},
},
userType: {
type: String,
required: [true, "usertype is required"],
trim: true,
lowercase: true,
enum: {
values: ["user", "admin"],
message: "each user can only be admin or user",
},
},
password: {
type: String,
required: [true, "please enter a password for your account"],
select: false,
minlength: [8, "your password could not be less than 8 characters"],
maxlength: [64, "your password could not be more than 64 characters"],
},
confirmPassword: {
type: String,
required: [true, "please confirm your password"],
validate: {
validator: function (val) {
return this.password === val;
},
message: "your passwords are not same",
},
},
email: {
type: String,
lowercase: true,
trim: true,
unique: [true, "this email has already been used!"],
},
phoneNumber: {
type: String,
unique: [true, "this phone number has already been used!"],
minlength: [11, "phone numbers has the length of 11 characters!"],
maxlength: [11, "phone numbers has the length of 11 characters!"],
},
gender: {
type: String,
required: [true, "gender field could not be empty!"],
enum: {
values: ["male,female"],
message: "you must be either male or female!",
},
},
birthDate: {
type: Date,
},
confirmedEmail: {
type: Boolean,
required: [true, "email confirmation must be specified as true or false"],
},
confirmedPhone: {
type: Boolean,
required: [
true,
"phone number confirmation must be specified as true or false",
],
},
userTickets: [
{
matchId: {
type: String,
required: [true, "matchId could not be empty!"],
},
ticketId: {
type: String,
required: [true, "matchId could not be empty!"],
},
},
],
});
const userModel = mongoose.model("users", usersSchema);
module.exports = userModel;
and this is my controller
exports.insertUser = async (req, res) => {
await userModel.create(req.body);
res.status(201).json({
status: "success",
message: "documents added to DB successfully",
});
};
I changed my model and it works but it doesn't work with this model
Upvotes: 1
Views: 71
Reputation: 5051
change the schema like this:
{
firstname: {
type: String,
required: [true, "first name is required"],
trim: true,
minlength: [
2,
"your first name could not be less than 2 characters!",
],
maxlength: [
25,
"your first name could not be more than 25 characters!"
]
},
lastname: {
type: String,
required: [true, "last name is required"],
trim: true,
minlength: [
2,
"your last name could not be less than 2 characters!",
],
maxlength: [
25,
"your last name could not be more than 25 characters!"
]
},
username: {
type: String,
unique: [true, "username has already been used!"],
required: [true, "username is required!"],
lowercase: true,
trim: true,
minlength: [
2,
"your user name could not be less than 2 characters!",
],
maxlength: [
35,
"your user name could not be more than 25 characters!"
]
},
userType: {
type: String,
required: [true, "usertype is required"],
trim: true,
lowercase: true,
enum: {
values: ["user", "admin"],
message: "each user can only be admin or user",
},
},
password: {
type: String,
required: [true, "please enter a password for your account"],
select: false,
minlength: [8, "your password could not be less than 8 characters"],
maxlength: [64, "your password could not be more than 64 characters"],
},
confirmPassword: {
type: String,
required: [true, "please confirm your password"],
validate: {
validator: function (val) {
return this.password === val;
},
message: "your passwords are not same",
},
},
email: {
type: String,
lowercase: true,
trim: true,
unique: [true, "this email has already been used!"],
},
phoneNumber: {
type: String,
unique: [true, "this phone number has already been used!"],
minlength: [11, "phone numbers has the length of 11 characters!"],
maxlength: [11, "phone numbers has the length of 11 characters!"],
},
gender: {
type: String,
required: [true, "gender field could not be empty!"],
enum: {
values: ["male", "female"],
message: "you must be either male or female!",
},
},
birthDate: {
type: Date,
},
confirmedEmail: {
type: Boolean,
required: [true, "email confirmation must be specified as true or false"],
},
confirmedPhone: {
type: Boolean,
required: [
true,
"phone number confirmation must be specified as true or false",
],
},
userTickets: [
{
matchId: {
type: String,
required: [true, "matchId could not be empty!"],
},
ticketId: {
type: String,
required: [true, "matchId could not be empty!"],
},
},
],
}
because for using of minlength
and maxlength
is used this format :
minlength: [
2,
"your first name could not be less than 2 characters!",
]
use values: ["male","female"]
for gender instead of values: ["male,female"]
because you are missing "
in the sample of body request
{
"lastname": "name",
"firstname": "family",
"username": "asf3",
"userType": "admin",
"password": "123456789",
"confirmPassword": "123456789",
"email": "[email protected]",
"phoneNumber": "12123456789",
"gender": "female",
"birthDate": "2002-06-05",
"confirmedEmail": true,
"confirmedPhone": true,
"userTickets": []
}
in the sample of req.body you use firstName
and lastName
with uppercase N
but in the schema are firstname
and lastname
use try/catch like this:
exports.insertUser = async (req, res) => {
try {
let newUser = new userModel(req.body)
await newUser.save();
res.status(201).json({
status: "success",
message: "documents added to DB successfully",
});
} catch (error) {
console.log(error);
res.status(500).json({
status: "faild",
message: "something went wrong",
});
}
};
Upvotes: 1