I've been searching for quite some time now, and I haven't found a similar issue to the one I am facing.
Working on a MERN stack application with four routes, three are functioning properly. However, the fourth route and the database model are causing me trouble.
Below is the model:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
let bucketListItem = new Schema({
itemOnList: { type: String, required: true },
status: { type: String, required: true },
dateCreated: { type: Date, default: Date.now },
completed: { type: Boolean, default: false }
});
module.exports = mongoose.model("bucketListItem",
bucketListItem);
The JSON used in Postman for testing the update function is as follows (derived from a GET request):
{
"completed": true,
"_id": "5c8b05c701eb8007dceb3aaa",
"itemOnList": "Visit Venice",
"status": "btdt.",
"dateCreated": null,
"__v": 0
}
Despite all fields being filled as per the model, updating any field results in an error indicating that the two 'required' fields must be filled.
Error message received:
{
"error": {
"errors": {
"status": {
"message": "Path `status` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `status` is required.",
"type": "required",
"path": "status"
},
"kind": "required",
"path": "status"
},
"itemOnList": {
"message": "Path `itemOnList` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `itemOnList` is required.",
"type": "required",
"path": "itemOnList"
},
"kind": "required",
"path": "itemOnList"
}
},
"_message": "bucketListItem validation failed",
"message": "bucketListItem validation failed: status: Path `status` is required., itemOnList: Path `itemOnList` is required.",
"name": "ValidationError"
}
}
Removing the 'required: true' from the model eliminates the error message, but also wipes data from the database entry completely.
{
"completed": false,
"_id": "5c8b05c701eb8007dceb3aaa",
"__v": 0,
"dateCreated": "null"
}
The code for my route:
blRoutes.route("/update/:id").post(function(req, res) {
BucketListItem.findById(req.params.id, function(err,
bucketListItem) {
if (!bucketListItem) {
res.status(404).send("Data not found.");
} else {
bucketListItem.itemOnList = req.body.itemOnList;
bucketListItem.status = req.body.status;
bucketListItem.dateCreated = req.body.dateCreated;
bucketListItem.completed = req.body.completed;
bucketListItem
.save()
.then(bucketListItem => {
res.json("Updated.");
})
.catch(err => {
res.status(400).send({ error: err });
console.log(err);
});
}
});
});
app.use("/bucketList", blRoutes);
Body Parser and CORS have been implemented in my setup.
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
Any feedback or help is greatly appreciated.
UPDATE:
A refactored version of my route:
blRoutes.route("/update/:id").post(function(req, res) {
BucketListItem.findOneAndUpdate({ _id: req.params.id }, req.body, {upsert: true, returnNewDocument: true})
.then(bucketListItem => res.json(bucketListItem))
.catch(err => res.status(404).send(err));
});
Additionally, the model has been updated slightly:
let bucketListItem = new Schema({
itemOnList: { type: String, required: true },
status: { type: String, required: true },
dateCreated: { type: Date, default: Date.now() },
completed: Boolean
});
Running this code does not yield the desired changes in the document. The data remains unchanged.
{
"dateCreated": null,
"_id": "5c8b05c701eb8007dceb3aaa",
"completed": false,
"itemOnList": "Visit Venice",
"status": "Bought tickets.",
"__v": 0
}
Your assistance is highly valued. Thank you for your support.