角度误差对象未定义。可能的原因:丢失范围,异步

时间:2019-01-03 03:03:14

标签: angularjs

// get all the schedule by weekly.
    getAllScheduleWeek() {
        this._apiService.get(this.connStr + '/Schedule/GetWeeks/')
            .subscribe(async weeks => {
                this.weeks = await weeks;
                this.currentWeek = this.weeks.filter(
                    week => week.WeekStart.split(' ')[0] === this.mondayOfCurrentWeek);
                const index = this.weeks.findIndex(
                    x => x.WeekStart === this.currentWeek[0].WeekStart);
                this.storeCurrentweekIndex = index;
                this.currentWeekIndex = index;
                this.weekId = this.currentWeek[0].WeekId;
                this.LongDesc = this.currentWeek[0].LongDesc;
                this.getWeekStatus(this.weekId);
            },
            error => this.msg = <any>error);
    };

我收到一条错误消息,“无法读取未定义的Weekstart”。

week.WeekStart =“ 12/31/2018 12:00:00 am”

this.weeks =一个数组。一个元素的示例:{WeekId:1,WeekStart:“ 1/3/2000 12:00:00 AM”,WeekEnd:“ 1/7/2000 12:00:00 AM”,LongDesc:“ 1月3日-1月7日2000“}

这并不总是错误。我真的无法弄清楚为什么有时候有时候行得通,有时候却行不通。我对angular2 +很陌生

“这个”会失去范围吗? 什么是.subscribe?我该如何将其更改为异步?

在此处定义当前周的星期一:

ngOnInit() {
        this.mondayOfCurrentWeek = this.getStartDayforSchedule(new Date());
        this.getAllScheduleWeek();
        this.getLocation();
    };




  // get the start day of a week.
    getStartDayforSchedule(currentDate) {
        // date set as without time
        currentDate.setHours(0, 0, 0, 0);
        // convert in to utc for all the time zone
        const utc = new Date(currentDate.getTime() + currentDate.getTimezoneOffset() * 60000);
        const day = utc.getDay(),
            diff = utc.getDate() - day + (day === 0 ? -6 : 1);
        const MondayOnCurrentWeek = new Date(utc.setDate(diff));
        const month = MondayOnCurrentWeek.getUTCMonth() + 1;
        const dayOfMonday = MondayOnCurrentWeek.getUTCDate();
        const year = MondayOnCurrentWeek.getUTCFullYear();
        return month + '/' + dayOfMonday + '/' + year;

    };

0 个答案:

没有答案