选择时服务中未定义的错误

时间:2018-04-18 11:09:10

标签: angular angular-services

我有这种方法可以提取一年的过滤数据列表。在选择我的HTML时我有两年:2018年和2017年,默认情况下我从2018年获得数据,那里很好,但是当我在2017年点击并在2018年再次点击时,我错过了一个错误,因为未定义,我不能发现失败。

getControlEstadoMes(anyo: number) {
    this.service.getControlEstadoMes(anyo).subscribe(data => {
      this.gridData = data;
    });
  }

这是服务上的方法:

getControlEstadoMes(value: number): Observable<ControlEstadoMesFormateado[]> {
        let listaControlEstadoMesFormateado : ControlEstadoMesFormateado[] = [];        
        let anyos = MOCK_CONTROLESTADOMES.filter(element => element.anyo == value);
        anyos.forEach(element => {
            let controlEstadoMesFormateado: ControlEstadoMesFormateado = Object.assign(element);
            let nombreMes = MOCK_CONTROLESTADOMESES.find(x => x.id == element.mes);
            controlEstadoMesFormateado.mes = nombreMes.nombre;
            listaControlEstadoMesFormateado.push(controlEstadoMesFormateado);
        });
        return of(listaControlEstadoMesFormateado);
    }

这是HTML上的选择:

<select class="form-control" id="anio" [(ngModel)]="anyoSeleccionado" (change)="getControlEstadoMes($event.target.value)">
          <option *ngFor="let anyo of listaAnyos">{{ anyo }}</option>  
        </select>

这是错误:

Cannot read property 'nombre' of undefined
    at eval (control-estado-mes.service.mock.ts:24)
    at Array.forEach (<anonymous>)
    at ControlEstadoMesServiceMock.getControlEstadoMes (control-estado-mes.service.mock.ts:21)
    at ControlHorasBloquearMesComponent.getControlEstadoMes (control-horas-bloquear-mes.component.ts:29)
    at Object.eval [as handleEvent] (ControlHorasBloquearMesComponent.html:10)
    at handleEvent (core.es5.js:12023)
    at callWithDebugContext (core.es5.js:13493)
    at Object.debugHandleEvent [as handleEvent] (core.es5.js:13081)
    at dispatchEvent (core.es5.js:8615)
    at eval (core.es5.js:9226)

0 个答案:

没有答案