I want to mark a as turf booked or not using put request. I am unable to update the data. I am using .save() method to update the data.
app.put("/turf/:turfId/day/:dayIndex/time-slot/:timeSlotId", async (req, res) => {
const tid = parseInt(req.params.turfId);
const dayIndex = parseInt(req.params.dayIndex);
const timeSlotId = parseInt(req.params.timeSlotId);
const booked = req.body.booked;
const turf = await turfData.findOne({ turfId: tid });
if (!turf) {
res.status(404).send("Turf not found");
return;
}
const day = turf.days[dayIndex];
if (!day) {
res.status(404).send("Day not found");
return;
}
const timeSlot = day.timeSlots.find((t) => t.id === timeSlotId);
if (!timeSlot) {
res.status(404).send("Time slot not found");
return;
}
timeSlot.booked = booked;
await turf.save();
res.send(turf);
});
I am getting response 200 OK on thunder client but I dont know why it is not getting updated in monogoDB.
Someone who might have gone through similar issue.
2
Answers
I can see that you are saving the turf but to save it in the DB, you have to create it too.
You can try this.
Try to reference the element to update directly like so: