无法访问此功能中的参数

时间:2016-03-30 14:58:07

标签: javascript node.js mongodb express mongoose

我对param 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内访问它,但似乎无法访问它。继续说它未定义&gt;&lt;。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在此之后:

// Can't access timesheet here
console.log("Kieran : " + timesheet.timesheet.start[0]);
Times.create(timesheet, function (err, newTimes, timesheet) {
    ...

时间表将是 Times.create()函数传递给回调函数的任何内容(您使用函数声明的匿名函数(错误) ,newTimes,时间表){

如果您的 console.log(&#34; Kieran:&#34; + timesheet.timesheet.start [0]); 正在记录未定义,那么不会成为问题。我看到的唯一区别是,在您可以访问时间表的部分中,您有:

console.log(timesheet.timesheet.start[1]);

而你没有:

console.log("Kieran : " + timesheet.timesheet.start[0]);

这意味着可能 .start [1] 已定义,但 .start [0] 未定义。

(有点难以遵循代码。如果你删除与你的问题无关的东西,比如为newTimes属性分配值,它会有所帮助。)