Cannot read property 'unsubscribe' of undefined in Angular 2

Discussão em 'Angular' iniciado por Alex Palacios, Novembro 20, 2024.

  1. I am trying to create an ObservableTimer that ticks up to a certain number. I already have logic to do that, but when I try to unsubscribe from it, I get a "Cannot read property 'unsubscribe' of undefined" error for it.

    Here is my code:

    syncExpireTime() {
    let route = AppSettings.API_ENDPOINT + 'Account/ExpireTime'
    this.http.get(route, this.options).map(this.extractData).catch(this.handleError).subscribe(
    res => {this.expireTime = +res},
    error => console.log(error),
    () => this.timerSub = TimerObservable.create(0,1000)
    .takeWhile(x => x <= this.expireTime)
    .subscribe(x => x == this.expireTime ? this.logout() : console.log(x))
    )
    }


    And then, here is my logout code. I am trying to unsubscribe from the expiration timer when I log out

    logout() {
    this.timerSub.unsubscribe()
    this.router.navigate(['./login'])
    }

    Continue reading...

Compartilhe esta Página