I am currently working on enhancing a fully functioning CRUD app by adding a new feature that allows users to update a list of vendors. They have the ability to add new vendors, update existing ones, and delete vendors. While the add and delete functions are working perfectly, I am encountering an issue with the update functionality. Despite using a similar method that I have successfully implemented elsewhere in the app for CRUD operations, the update is not functioning as expected. Here is the snippet of the code causing the issue:
// async function from AXIOS request
const { original, updatedVendor } = req.body;
let list = await Vendor.findOne({ id: 1 });
if (!list) return res.status(500).json({ msg: 'Vendors not found' });
let indexOfUpdate = list.vendors.findIndex(
(element) => element.id === original.id
);
list.vendors[indexOfUpdate].id = updatedVendor.id;
list.vendors[indexOfUpdate].name = updatedVendor.name;
const updated = await list.save();
res.json(updated);
Despite console logging that the list.vendors
array is being updated, the save()
method is not reflecting the changes in the existing document on the database side.
EDIT:
I have also tried using an alternative format for the save()
method, like this:
list.save().then(res.json(list));
EDIT 2: In response to inquiries about viewing logs, I am unable to share the complete console.log(list.vendors) as it contains sensitive information. However, I can confirm that the modifications made to the list are visible when I execute the following in the VendorSchema:
VendorSchema.post('save', function () {
console.log(util.inspect(this, { maxArrayLength: null }));
});
Despite this, the changes are still not being reflected on the database side after saving.