繁体   English   中英

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

[英]Angular Error object undefined. Possible causes: lost scope, async

// 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? 我该如何将其更改为异步?

这是定义mondayofcurrentweek的地方:

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;

    };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM