Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda