Угловая ошибка объекта не определена. Возможные причины: потеря прицела, асинхронность - PullRequest
0 голосов
/ 03 января 2019
// 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);
    };

Я получаю сообщение об ошибке "не могу прочитать недельный старт из неопределенного".

week.WeekStart = "31.12.2008, 12:00:00"

this.weeks = массив. пример одного элемента: {WeekId: 1, WeekStart: "1/3/2000 12:00:00 AM", WeekEnd: "1/7/2000 12:00:00 AM", LongDesc: "3 января - 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;

    };
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...