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

184
Visualizações
Nodejs | Object property is available but it's returning 'undefined'

I've been trying to solve this for about 2 hours. I am returning an object in Nodejs. This object has 3 properties. I can access the first two, but when I try to access the third property, I get the 'undefined' warning. I'm about to go crazy can you help me please?

Request:

  const categories = await Categories.find({}); //mongoose query
  console.log(categories[0]); //its returning Object
  console.log(categories[0].category); //its returning  []
  console.log(categories[0]._id); //its returning  new ObjectId("6257233b897c3b8785ff625b")
  console.log(categories[0].mainCategories); //its returning  undefined ?? wtf?!
  console.log(categories[0].hasOwnProperty("category")); //its returning false
  console.log(categories[0].hasOwnProperty("_id")); //its returning false
  console.log(categories[0].hasOwnProperty("mainCategories")); //its returning false

Object Result:

{
  category: [],
  _id: new ObjectId("6257233b897c3b8785ff625b"),
  mainCategories: [
    { id: 1000010100, name: 'Konut', subCategories: [Array] },
    { id: 1000010200, name: 'Devremülk', subCategories: [Array] },
    { id: 1000020100, name: 'İşyeri', subCategories: [Array] },
    { id: 1000020200, name: 'Arazi', subCategories: [Array] },
    {
      id: 1000020300,
      name: 'Turistik İşletme',
      subCategories: [Array]
    }
  ]
}

enter image description here

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

0

The answers given by Cristian Traìna and VebDav caused lightning in my brain. I couldn't find the exact solution to the problem on the internet. But different problems guided me. The problem stems from my stupidity. I forgot to enter the mainCategories field in the mongoose category schema. I added this and its fixed. Thank you so much everyone for your answers.

enter image description here

And now its returning mainCategories :)

enter image description here

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