我想将新数据插入到集合中,但是在提交表单时,出现验证错误。
ValidationError:教师验证失败:主题:请输入主题,second_subject:路径
second_subject
是必需的。
HTML文件
<form action="/register" method="post" id="form3">
<div class="form-group"><label class="small mb-1">Full name</label><input class="form-control py-4" style="width: 50%;" type="name" name="name" placeholder="Enter full name" /></div>
<div class="form-group"><label class="small mb-1" >Email</label><input class="form-control py-4" style="width: 50%;" type="username" name="username" placeholder="Enter email address" /></div>
<div class="form-group"><label class="small mb-1" >Password</label><input class="form-control py-4" style="width: 50%;" type="password" name="password" placeholder="Password" /></div>
<input type="hidden" id="roles" name="roles" value="Creator">
<div class="form-group"><label class="small mb-1" >Select Primary Subject</label>
<select name="subject" class="form-control" style="width: 30%;" id="subject">
<option value="">-- Select Subject --</option>
<% subject.forEach(function (subject) { %>
<option value= "" ><%= subject.subject_name %></option>
<% }) %>
</select>
</div>
<div class="form-group"><label class="small mb-1" >Select Secondary Subject</label>
<select name="secondary_subject" class="form-control" style="width: 30%;" id="secondary_subject">
<option value="">-- Select Subject --</option>
<% subject.forEach(function (subject) { %>
<option value= "" ><%= subject.subject_name %></option>
<% }) %>
</select>
</div>
<div class="form-group"><label class="small mb-1" >Preferred Level of study</label>
<select name="level" class="form-control" style="width: 30%;" id="level">
<option value="">-- Select level --</option>
<option value="Level 1">Level 1</option>
<option value="Level 2">Level 2</option>
</select>
</div>
<div class="form-group mt-4 mb-0"><input type="submit" value="Submit" style="width: 20%;" a class="btn btn-primary btn-block" href="/admin_user"></div>
</form>
路线
if(req.body.subject && req.body.secondary_subject,req.body.level){
var teacherData = {
subject: req.body.subject,
second_subject: req.body.secondary_subject,
level: req.body.level,
userId:req.session.userId,
}
Teacher.create(teacherData, function (err, teacher){
if(err){
return next(err)
}else
{
console.log('Teacher details added'+teacher)
return res.redirect('/admin_user');
}
});
}else{
var err = new Error('All fields have to be filled out');
err.status = 400;
return next(err);
}
架构
var TeacherSchema = new mongoose.Schema ({
subject: {
type:String,
required:true,
},
level: {
type:String,
required:true,
},
second_subject: {
type:String,
required:true,
},
userId:{
type:String,
required:true,
},
});
答案 0 :(得分:1)
在路由文件中,检查“ not null”和“不等于空字符串”。这样可以确保不将空字符串输入到您的数据库中。
if (req.body.subject != null && req.body.subject !== "" && req.body.secondary_subject != null && req.body.secondary_subject !== "" && req.body.level != null && req.body.level !== "") {
var teacherData = {
subject: req.body.subject,
second_subject: req.body.secondary_subject,
level: req.body.level,
userId: req.session.userId,
}
Teacher.create(teacherData, function(err, teacher) {
if (err) {
return next(err)
} else {
console.log('Teacher details added' + teacher)
return res.redirect('/admin_user');
}
});
} else {
var err = new Error('All fields have to be filled out');
err.status = 400;
return next(err);
}