我在我的node.js应用程序中有以下javascript代码。但某些对象没有存储在我的
这段代码有什么问题吗?
变量
以下命令:
appointment
变量中。即使我设置它们,当我直接访问它们时,它也可以正常工作:console.log(appointment.test);
。这段代码有什么问题吗?
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);
下面是生成的输出结果:
{ 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: [] }
变量
console.log(appointmentsDB [i])
看起来像这样:{ comment: 'fsadsf',
room: 'dqfa',
reqAssi: 3,
maxStud: 20,
timeSlot: 8,
week: 31,
year: 2013,
day: 3,
_id: 51f957e1200cb0803f000001,
students: [],
assis: [] }
以下命令:
console.log(Object.getOwnPropertyNames(appointmentsDB[i]), Object.getOwnPropertyNames(Object.getPrototypeOf(appointmentsDB[i])));
展示:
[ '_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' ]
但是,我希望我的最后输出也提供test、subscribed、enoughAssis和studentSlotsOpen这些条目。这段代码有什么问题吗?
我找到的解决方法是手动复制我想要的元素。
appointmentsDB[i]
是什么? - BergitoString
或toJSON
方法吗? - Bergiappointment
。(我将i
赋值为0,并将您指定的对象分配给appointmentsDB[0]
。) - Ted Hopp