Как исправить Невозможно прочитать свойство 'next' из undefined? - PullRequest
2 голосов
/ 22 апреля 2019

В сервисе API я хочу добавить ошибку в уведомление.processError метод вызывается в запросе http с привязкой: catchError(error=>this.processError(error)),

api.service:

private processError(res: HttpErrorResponse) {
    this.currentErrorSubject.next(res);
    console.log('ERROR', res)
    // this.snackBar.open(`${res.message}`, 'Hide', {duration:10000, horizontalPosition: 'right', panelClass:['error-snack']})
    let notification = new Notification(1, 'error', 'HELLO KITTY')
    this.notificationService.addNotification(notification)

    return observableThrowError(res);
  }

messages.service:

@Injectable({
  providedIn: 'root'
})
export class NotificationService {
  isOpen : boolean
  private notifications = new Array<Notification>();
  public notificationsSubject: ReplaySubject<Notification[]>;
  constructor(
    private snackBar: MatSnackBar,
  ) { }

  open() {
    this.isOpen=true

  }

  addNotification(notification: Notification) {      

        this.notifications = [...this.notifications, notification];
        console.log('NOTIF', this.notifications)

        this.notificationsSubject.next(this.notifications);
    }

Произошла ошибкав функции 'addNotification', где this.notificationsSubject не определено.Можете ли вы объяснить, почему он не определен?Спасибо

1 Ответ

2 голосов
/ 22 апреля 2019

Вы должны инициализировать notificationsSubject значением по умолчанию:

public notificationsSubject: ReplaySubject<Notification[]> = new ReplaySubject({ /* ... */ })
...