Nodejs | Object property is available but it's returning 'undefined'

Viewed 43

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

1 Answers

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

Related