Reputation: 415
I'm developing a REST API with Node JS. I have developed post request but don't try to save data on a specific object of MongoDB Schema. I have this MongoDB schema
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}
}
});
and this my post request:
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'}});
});
});
How can i save the data into "demographics" object? My code give erro 500. Any help please, is very importat for me to fix this problem.
Best
This error log:
{ 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.
Upvotes: 2
Views: 88
Reputation: 4993
Can you log your req.body
?
Your schema definition has few fields marked as required and I am afraid you are not passing to them or they are going undefined, eg: crop, liveStock, etc Here's what you can do:
Or
Upvotes: 1