'JavaSript: mongoose issue with trying to delete an item in an array with splice and update it with findByIdAndUpdate
I am currently trying to delete an item from an array with splice and update it with findByIdAndUpdate. Now the issue is, when updating, it doesn't delete the item I specified with the splice part, it deletes every item out of the one I specified.
selection = interact.values;
if (selection == "1") {
selection--;
selection = parseFloat(selection);
selectionUser = 1;
} else if (selection == "2") {
selection--;
selection = parseFloat(selection);
selectionUser = 2;
} else if (selection == "3") {
selection--;
selection = parseFloat(selection);
selectionUser = 3;
}
console.log(selection)
splicedReason = data.reason.splice(selection, 1)
splicedMod = data.moderator.splice(selection, 1)
await User.findByIdAndUpdate(wUser.id, {
reason: splicedReason,
moderator: splicedMod
})
console.log(splicedReason)
Now, the array contains the following items:
0: "test1"
1: "test2"
2: "test3"
For example if the selection is "3", console.log(selection) logs 2 and console.log(splicedReason) logs [ 'test3' ]. If I check the array, it looks like this:
0: "test3"
What's the issue here? Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
