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

131
Views
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 answers
Answer question

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 Report
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!