我正在尝试使用Postman在我的mongo数据库中插入一个条目,但是我会看到以下内容:
{"content":{"message":"Validation failed","name":"ValidationError","errors":{"name":{"message":"Path `name` is required.","name":"ValidatorError","path":"name","type":"required"}}}}
以下是我的API中的相关控制器:
var mongoose = require('mongoose');
var Course = mongoose.model('Course');
var sendJSONResponse = function(res, status, content){
res.status(status);
res.json({content});
}
module.exports.makeCourse = function(req, res){
Course.create({
name: req.body.courseName
},function(err, course){
if(err){
sendJSONResponse(res, 400, err);
} else{
sendJSONResponse(res, 201, course);
}
})
};
module.exports.listCourses = function(req, res){
Course.find({}, function(err, course){
sendJSONResponse(res, 200, course);
})
};
module.exports.courseIDLookup = function(req, res){};
module.exports.deleteCourse = function(req, res){};
以及数据模型:
var mongoose = require('mongoose')
var subject = require('./subject.js');
var courseSchema = new mongoose.Schema({
name : {type: String,
unique: true,
required: true},
subjects : [subject.schema]
});
module.exports = mongoose.model('Course', courseSchema);
创建此条目时我犯了什么错误?
答案 0 :(得分:1)
如果name
是您的关键,
name: req.body.courseName
应该是
name: req.body.name