0

I'm working on somebody's code and the author isn't available rn. I have an object with another object nested in it, and it's been retrieved from api call. The problem is when i do a console.log on the parent object it shows the nested object in it properly but when i try to log object.object i'm getting an empty object. Any idea on what could cause this problem?

console.log(product) ==> {id: 1, extra:{name:'example'}
console.log(product.extra) ==> {}
Nahid OG
  • 131
  • 4
  • 16
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your code. – kennarddh Aug 21 '22 at 06:43
  • That's not possible, it's a massive project and there are so many inheritances and dependencies in between. I can't tell which part is producing the problem – Nahid OG Aug 21 '22 at 06:48
  • 2
    I think every time I've seen this, the problem was that the browser logs show the object in the current state, not the state when it was logged. You can try to `console.log(JSON.stringify(product))` to see how it looks at the time of logging. If it is also empty there, then it is likely modified later on (between logging, but before displaying in the browser console logs). [This fiddle](https://jsfiddle.net/yav04uko/) shows what I mean if you look at the browser console logs – A_A Aug 21 '22 at 06:50
  • I try to reproduce your problem. The code works like expected. [jsfiddle](https://jsfiddle.net/ds8kf31L/1/) – kennarddh Aug 21 '22 at 06:50
  • @A_A thank you, this was really helpful. Actually it is also empty in string format. – Nahid OG Aug 21 '22 at 07:02
  • Glad it helped. I will search for the corresponding question and mark it as a duplicate – A_A Aug 21 '22 at 08:13

0 Answers0