diff --git a/src/mqtt.service.ts b/src/mqtt.service.ts index 59d3be5..b096946 100644 --- a/src/mqtt.service.ts +++ b/src/mqtt.service.ts @@ -144,14 +144,16 @@ export class MqttService { () => { const subscription: Subscription = new Subscription(); this.client.subscribe(filterString, (err, granted: ISubscriptionGrant[]) => { - granted.forEach((granted_: ISubscriptionGrant) => { - if (granted_.qos === 128) { - delete this.observables[granted_.topic]; - this.client.unsubscribe(granted_.topic); - rejected.error(`subscription for '${granted_.topic}' rejected!`); - } - this._onSuback.emit({ filter: filterString, granted: granted_.qos !== 128 }); - }); + if(granted) { // granted can be undefined when an error occurs when the client is disconnecting + granted.forEach((granted_: ISubscriptionGrant) => { + if (granted_.qos === 128) { + delete this.observables[granted_.topic]; + this.client.unsubscribe(granted_.topic); + rejected.error(`subscription for '${granted_.topic}' rejected!`); + } + this._onSuback.emit({filter: filterString, granted: granted_.qos !== 128}); + }); + } }); subscription.add(() => { delete this.observables[filterString];