Разобрать массив JSON с Moment.js в функции подписки - PullRequest
0 голосов
/ 12 сентября 2018

Я получил несколько time элементов в массиве data JSON, которые я хочу проанализировать и отформатировать с помощью Moment.js и сохранить их в массиве this.appointmentTimes, к которому я могу легко получить доступна мой взгляд с {{appointmentTime.time}}.

Это мой JSON:

[
  { "time":"2018-10-22T14:30:00+0200", "slotsAvailable":1 },
  { "time":"2018-10-22T14:45:00+0200", "slotsAvailable":1 },
  { "time":"2018-10-22T15:00:00+0200", "slotsAvailable":1 }
]

После обновления страницы я получаю эту ошибку:

ERROR in src/app/pages/booking/form/booking.component.ts(75,9): error TS2322: Type 'string' is not assignable to type 'object[]'.
src/app/pages/booking/form/booking.component.ts(76,22): error TS2345: Argument of type 'object[]' is not assignable to parameter of type 'string'.

Это моя соответствующая функция, которая будет выполняться через мой взгляд.

private appointmentLocations: Array<object> = [];
private appointmentTypes: Array<object> = [];
private appointmentTimes: Array<object> = [];

onChangeTypeId(TypeId) {
    console.log(TypeId);
    this.selectedAppointmentTypeId = TypeId;
    this.apiService
      .getAppointmentTimesById(
        this.selectedAppointmentTypeId,
        this.selectedAppointmentLocation,
        this.selectedDatePicker
      )
      .subscribe((data: Array<object>) => {
        /*this.appointmentTimes = data; */
        this.appointmentTimes = JSON.stringify(
          JSON.parse(data).map(function(data) {
            var pattern = 'HH:mm';
            data = moment(data.time).format(pattern);
            return data;
          })
        );
        console.log(data);
      });
  }
}

1 Ответ

0 голосов
/ 12 сентября 2018

.map(response => response.json()) перед подпиской, поскольку ответом является строка, а не JSON.

...