I'm deleting an item from an array with mongoose. Some code:
const { find, remove } = require('lodash');
....
UserSchema.methods.deleteItem = async function (id) {
const user = this;
const item = find(user.items, i => i.id === id);
const idx = user.items.indexOf(item);
user.items.splice(idx, 1);
// remove(user.items, i => i.id === id);
try {
await user.save();
return item;
} catch (err) {
throw new Error(err);
}
};
In the above code, I use splice(), which works correctly. However, lodash's remove doesn't work. The documentation for .remove() says that it mutates the array directly, so why does it not work here?