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

130
Visualizações
Array not inserting object correctly

I am creating a website. I am trying to add a "Warn" feature, but when I store the warn into a user, I get this: 0: 1

Here is what I mean by 0: 1 So, here is my code... The variable that is causing this problem is newUser[0].warn = newWarn

const newMsg = {
        title: msg[0].title,
        body: msg[0].body,
        author: msg[0].author,
        postid: msg[0].postid
    }
    const warn = newUser[0].warn
    const newWarn = warn.push(newMsg)
    console.dir(newMsg)
    newUser[0].warned = true
    newUser[0].warn = newWarn
    await newUser[0].save()

Btw, newMsg is set correctly: enter image description here

And here is where it says 0: 1

enter image description here

enter image description here

This is supposed to be storing newMsg in an array, while keeping the other contents in the array.

Thanks in advance :)

EDIT:

I found the problem, but I am unsure how to fix it.

It has to do with const newWarn = warn.push(newMsg)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is because warn.push return the array of length which is actually 1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

about 4 years ago · Juan Pablo Isaza Relatório
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