Я разрабатываю REST API с помощью Node JS.
Я разработал пост-запрос, но не пытаюсь сохранить данные об определенном объекте схемы MongoDB.
У меня есть эта схема MongoDB
var ProfileSchema = db.Schema({
farmId: { type: String, required: true },
companyName: { type: String, required: true },
firstname: {type: String, required: true},
surname: {type: String, required: true},
address: {type: String, required: true},
city: {type: String, required: true},
state: {type: String, required: false},
postalcode: {type: String, required: true},
country: {type: String, required: true},
telephone: {type: String, required: true},
email: {type: String, required: true},
demographics:{
farmSize: {type: String, required: true },
crops: {type: String, required: true},
liveStock: {type: String, required: true},
precisionFarming: {type: String, required: true},
currentTire: {type: String, required: false}
}
});
и это мой запрос:
router.post('/profile', VerifyToken, function (req, res) {
Profile.create({
farmId:req.body.farmId,
companyName:req.body.companyName,
firstname:req.body.firstname,
surname: req.body.surname,
address: req.body.address,
city: req.body.city,
state: req.body.state,
postalcode: req.body.postalcode,
country: req.body.country,
telephone: req.body.telephone,
email: req.body.email,
farmSize: req.body.farmSize,
crops: req.body.crops,
liveStock: req.body.liveStock,
precisionFarming: req.body.precisionFarming,
currentTire: req.body.currentTire
},
function (err, profile) {
if (err) return res.status(500).send("There was a problem adding the information to the database.");
res.status(200).send({status: 'ok', data: { msg: 'Profile saved'}});
});
});
Как сохранить данные в объекте «демография»?
Мой код дает 500 ошибок.
Любая помощь, пожалуйста, очень важна для меня, чтобы решить эту проблему.
Лучший
Этот журнал ошибок:
{ ValidationError: ProfileFarm validation failed: demographics.precisionFarming: Path `demographics.precisionFarming` is required., demographics.liveStock: Path `demographics.liveS
tock` is required., demographics.crops: Path `demographics.crops` is required., demographics.farmSize: Path `demographics.farmSize` is required.