From 83b59edf5bdec12f3d37e4791f2c4ea96a49902b Mon Sep 17 00:00:00 2001 From: Yennick Trevels Date: Tue, 8 May 2018 21:17:00 +0200 Subject: [PATCH] verify existence of granted argument on subscribe callback --- src/mqtt.service.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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];