我的代码存在问题。尝试了一段时间从一个或多个对象获取值。我正在使用循环来从对象中获取数组中的每个值。当我在值上硬编码位置时,我得到了值,但是当我在循环中使用值'num'时未定义?
for(var num = 0; num <= 6; num++){
// sets the date from the start of the week.
newDate = moment().startOf('week').weekday(num).toDate();
if(timesheet.timesheet.start[num] === ''){
....
} else {
Times.create(timesheet, function(err, newTimes) {
if(err){
console.log(err);
} else {
newTimes.timesheet.id = newTimesheet.id;
// Prints the value fine
console.log(timesheet.timesheet.start[0]);
// Gives me undefined?
newTimes.start = timesheet.timesheet.start[num];
newTimes.end = "On";
newTimes.save();
newTimesheet.times.push(newTimes);
newTimesheet.save();
}
});
}
}