TheUnreal TheUnreal - 4 days ago 7
TypeScript Question

Subscribe after Unsubscribe()

I have an array of observables.

On my

ngOnInit()
method, I initialize my subscriptions.

this.lineOrdersSub[line.$key] = this.af.database.list(this.businessService.path + 'line_orders/' + line.$key)
.subscribe((lineOrders) => {
this.line_orders[line.$key] = lineOrders;
});


I also have a checkbox for each subscription. When the checkbox is ticked (default), the subscription is active (subscribing to new data).

When I uncheck a checkbox, I do this:

unCheckmethod() {
this.lineOrdersSub[line.$key].unsubscribe(); //Unsubscribe to my subscription
this.line_orders[line.$key] = null; // Remove the data I get after the subscription (See first code snippet)
}


My uncheck method works well, but after I'm trying to start the subscription again with:

checkMethod() {
this.lineOrdersSub[line.$key].subscribe();
}


I get:

this.lineOrdersSub[line.$key].subscribe is not a function


My question:


  1. How I can subscribe to a subscription after I
    unsubscribe
    ? If impossible, Can you think of any other way to do this?


Answer

The error is correct. You're calling:

this.lineOrdersSub[line.$key] = this.af.d ... .subscribe(...);

which means you're assigning a Subscription object to this.lineOrdersSub[line.$key]. Not an Observable.

Then calling .unsubscribe() obviously works as it should. When you try to call subscribe() it fails because Subscribtion object has no subscribe() method.

You have to subscribe to the Observable.

Comments