I am building a project in REST API. One of my models is reservation as described below.
const reservation = new mongoose.Schema({
customerName: {
type: String,
required: true,
minlength: 3,
maxlength: 50
},
date: { type: Date, required: true },
message: {
type: String,
minlength: 3,
maxlength: 250
}
})
I am building a PUT
route to update the reservation (shorter version below for simplicity)
router.put('/:id', async (req, res) => {
const { customerName, message, date } = req.body;
const reservation = await Reservation.findByIdAndUpdate(req.params.id, { customerName, message, date: new Date(date) }, {
new: true
});
res.send(reservation);
});
If the client doesn't pass the req.body.message
it becomes null
in the database. Which makes sense. But what is the way to, instead of overwriting the message to null, just ignore it, and keep the message as it is? There has to be a better way then writing if statements and conditions.