Is there a way in Mongoose to instruct it to not save the age field if it's null or undefined?
Or is there a method to achieve this in Express?
Express
router.put('/edit/:id', function(req, res) {
Person.findOneAndUpdate({ _id: req.params.id }, {
name: req.body.updateData.name,
age: req.body.updateData.age
}, { new: true });
})
Mongoose Schema
var PersonSchema = new Schema({
name: { type: String},
age: {type: String}
})
An explanation (if you're wondering why I need this)
I'm using the same HTML template for creating a new person and editing an existing person. When creating a new person, Mongoose will only save the name field if the age field is left empty. But when using the edit template, Mongoose always sets the age field as null, even if it's empty. I'm struggling to find a solution to prevent this behavior.