Property 'unsubscribe' does not exist on type 'Observable<DataSnapshot>'

15,257

Solution 1

Unsubscribe is a method on the subscription itself. Hence, add at the top:

private _chatSubscription;

then in your otherMethod:

this._chatSubscription = this._chatObserver.subscribe(...);

And in your destroy/leave/termination handler:

this._chatSubscription.unsubscribe();

Solution 2

try this

private _chatObserver: Observable<firebase.database.DataSnapshot>
private _subscription:Subscription
otherMethod () {
    this._chatObserver = this._chat.observe(alarmId)
    this._subscription=this._chatObserver.subscribe(
      (messageSnap: firebase.database.DataSnapshot) => {
        this.messages.push(messageSnap.val())
      },
      error => {throw error})
}

ionViewDidLeave() {
   this._subscription.unsubscribe() 
}
Share:
15,257

Related videos on Youtube

Jørgen Svennevik Notland
Author by

Jørgen Svennevik Notland

Hi. www.getflareapp.com

Updated on September 15, 2022

Comments

  • Jørgen Svennevik Notland
    Jørgen Svennevik Notland over 1 year

    Typescript(tslint in atom editor) is giving me a typescript error, but i cannot figure out how to set the correct type.

    Error message: enter image description here

    chat component:

      private _chatObserver: Observable<firebase.database.DataSnapshot>
    
      otherMethod () {
            this._chatObserver = this._chat.observe(alarmId)
            this._chatObserver.subscribe(
              (messageSnap: firebase.database.DataSnapshot) => {
                this.messages.push(messageSnap.val())
              },
              error => {throw error})
        }
    
        ionViewDidLeave() {
           this._chatObserver.unsubscribe() 
        }
    

    _chat provider:

      public observe (alarmId){
        let messagesRef = this._ref.child(`alarms/${alarmId}/messages`)
    
        const observable = Observable.create(observer => {
          messagesRef.on('child_added',(messageSnap) => {
                observer.next(messageSnap)
            },
            (error) => observer.error(error)
          )
          return () => {
            messagesRef.off('value')
          };
        });
    
        return observable
      }
    
    • cartant
      cartant over 6 years
      unsubscribe should be called on a Subscription - not on an Observable. Your call to subscribe will return a Subscription.