Console.log does not show expected object properties

I have the following code in javascript in my node.js. application However, some objects are not stored in my appointment variable. Even if I install them when I directly access them, it works: console.log(appointment.test);

What have I done wrong in this code?

 var appointment = { subscribed: false, enoughAssis: false, studentSlotsOpen: false }; console.log(appointment); for (var key in appointmentsDB[i]) { appointment[key] = appointmentsDB[i][key]; } appointment.test= "res"; console.log(appointment.test); console.log(appointment); 

And here is the result:

 { subscribed: false, enoughAssis: false, studentSlotsOpen: false } res { comment: 'fsadsf', room: 'dqfa', reqAssi: 3, maxStud: 20, timeSlot: 8, week: 31, year: 2013, day: 3, _id: 51f957e1200cb0803f000001, students: [], assis: [] } 

The variable console.log(appointmentsDB[i]) looks like this:

 { comment: 'fsadsf', room: 'dqfa', reqAssi: 3, maxStud: 20, timeSlot: 8, week: 31, year: 2013, day: 3, _id: 51f957e1200cb0803f000001, students: [], assis: [] } 

Next command:

 console.log(Object.getOwnPropertyNames(appointmentsDB[i]), Object.getOwnPropertyNames(Object.getPrototypeOf(appointmentsDB[i]))); 

Shows:

 [ '_activePaths', '_events', 'errors', '_maxListeners', '_selected', '_saveError', '_posts', 'save', '_pres', '_validationError', '_strictMode', 'isNew', '_doc', '_shardval' ] [ 'assis', 'timeSlot', 'db', '_schema', 'id', 'base', 'day', 'collection', 'reqAssi', 'constructor', 'comment', 'year', 'room', 'students', 'week', '_id', 'maxStud' ] 

However, I would expect my last output to also provide the test, signed, enoughAssis and studentSlotsOpen entries. What is wrong with this code?

The solution I found was to manually copy the elements that I wanted.

+4
source share
1 answer

You probably have a Document object instead of a simple object. They have their own toJSON method , which gives only the properties of your schema and _id , but nothing else. If you copy this method using the for-in-loop to the appointment object, it will be serialized differently during registration.

Try

 for (var key in appointmentsDB[i].toObject()) { appointment[key] = appointmentsDB[i][key]; } appointment.test= "res"; console.log(appointment); 
+3
source

Source: https://habr.com/ru/post/1494561/


All Articles