Skip to content
Advertisement

JavaScript: dynamic property not showing unless accessed explicitly

I’m using NodeJs, I’m creating a dynamic property inside an object, and then pushing it into an array. The problem is, when I access the variable via console.log(object[dynamic_property_name]) I can see it in the console, but when I console.log(object) the dynamic property doesn’t show.

here is the code, the property in question is teachers_matieres in the element object. PS: I console logged the property (explicitly) in each step of the code, even just before the return, it shows. But when I try to print the object it doesn’t show, neither in post man.

JavaScript

This is the output of postman:

JavaScript

I really need to append that property to each element, how can I do that? what’s wrong with the dynamic property?

Advertisement

Answer

The problem was that the objects returned from mongoose inherit from document, and they are not plain JSON objects, they use .toObject() method to obtain the json object. what i had to do was add .lean() in the query like so Model.findOne().lean().exec() it tells mongoose to return a plain json object.

reference: similar question

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement