Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!