我对作为对象的参数timesheet
有问题。我可以在评论区域中访问它:
"// Can access timesheet here"
但不是:
"// Can't access timesheet here"
码:
function d(obj, timesheet){
var newDate;
var timesheetParams = {
weekNumber: moment().week(),
year: moment().year(),
employee: obj.employee
};
Timesheet.create(timesheetParams, function(err, newTimesheet) {
if(err){
console.log(err);
} else {
console.log("Test: " + JSON.stringify(timesheet));
for(var num = 0; num <= 6; num++){
// sets the date from the start of the week.
newDate = moment().startOf('week').weekday(num).toDate();
// Can access timesheet here
console.log(timesheet.timesheet.start[1]);
if(timesheet.timesheet.start[num] === ''){
Times.create(timesheet.timesheet, function(err, newTimes, timesheet) {
if(err){
console.log(err);
} else {
newTimes.timesheet.id = newTimesheet.id;
newTimes.date = newDate;
newTimes.start = "OFF";
newTimes.end = "OFF";
newTimes.save();
newTimesheet.times.push(newTimes);
newTimesheet.save();
}
});
} else {
// Can't access timesheet here
console.log("Kieran : " + timesheet.timesheet.start[0]);
Times.create(timesheet, function(err, newTimes, timesheet) {
console.log("Kieran : " + timesheet.start[0]);
if(err){
console.log(err);
} else {
newTimes.timesheet.id = newTimesheet.id;
newTimes.date = newDate;
console.log("Day Printed: " + timesheet);
newTimes.start = timesheet.timesheet.start[num];
newTimes.end = timesheet.timesheet.end[num];
newTimes.save();
console.log("With numbers being put in: " + JSON.stringify(newTimes))
console.log("TESTER TIMES WITH NUMBERS: " + newTimes.times);
newTimesheet.times.push(newTimes);
newTimesheet.save();
}
});
}
//console.log("TESTER TIMES: " + newTimesheet.times[num].start[num]);
}
}
});
}
我已经尝试过一切在
if/else
中访问它的方法,但是似乎无法访问它。一直说它的未定义> 感谢您的帮助。
最佳答案
之后:
// Can't access timesheet here
console.log("Kieran : " + timesheet.timesheet.start[0]);
Times.create(timesheet, function (err, newTimes, timesheet) {
...
timesheet将是Times.create()函数传递给回调函数(您使用函数声明的匿名函数(err,newTimes,timesheet))的任何内容{
如果您的console.log(“ Kieran:” + timesheet.timesheet.start [0]);是未定义的日志记录,那么这将不是问题。我看到的唯一其他区别是,在可以访问时间表的部分中,您具有:
console.log(timesheet.timesheet.start[1]);
而您无法在哪里:
console.log("Kieran : " + timesheet.timesheet.start[0]);
这意味着可能已定义.start [1],但未定义.start [0]。
(很难遵循这些代码。如果您剔除与问题无关的内容,例如将值分配给newTimes属性,则将有所帮助。)