无法在pubnub-angular2中读取属性'forEach'

时间:2017-02-08 05:31:55

标签: angular pubnub

我在Angular2中使用Pubnub 实际上它运作良好,但几分钟后渲染后会出错。我不知道为什么会这样。

这是我的代码和截图。

constructor(private router: Router, private navService: NavService, pubnubService: PubNubAngular) {

    var pubnubService = new PubNubAngular();
    this.branchIdSave = localStorage.getItem('branchId');

    pubnubService.init({
      publishKey: p_key,
      subscribeKey: s_key
    });

    pubnubService.subscribe({
      channels: ['ADMIN-' + this.branchIdSave],
      withPresence: true,
      triggerEvents: ['message', 'presence', 'status']
    });

    pubnubService.getMessage('ADMIN-' + this.branchIdSave, function (msg) {
      this.newOrderCount = msg['message'].branchBadge['newOrderCount'];
      this.preservedOrderCount = msg['message'].branchBadge['preservedOrderCount'];
      this.waitingApplicantCount = msg['message'].branchBadge['waitingApplicantCount'];
    });
  }


  ngOnInit() {
    this.navService.getBadges(this.branchIdSave).subscribe(
      data => {
        this.newOrderCount = data.result.newOrderCount;
        this.preservedOrderCount = data.result.preservedOrderCount;
        this.waitingApplicantCount = data.result.waitingApplicantCount;
      }
    );
  }

enter image description here

  • 添加了错误消息

GET http://ps15.pubnub.com/v2/subscribe/sub-c-0956782e-e91e-11e6-8919-0619f8945 ... tr = 4& uuid = 04434f7f-c14d-46ec-84ba-e382a3db1a11& pnsdk = PubNub-JS-Web%2F4.4.0 net :: ERR_EMPTY_RESPONSE

error_handler.js:56 TypeError:无法读取未定义的属性'forEach'     at Object.self.listener。(匿名函数)[作为状态](http://localhost:4200/vendor.bundle.js:140472:40)     在https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:2:20403     at Array.forEach(native)     在e.value(https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:2:20371)     在e._reconnectionCallback(https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:2:17503)     在https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:2:21751     在m(https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:2:26169)     在https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:1:9488     在f.callback(https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:1:15834)     在f。 (https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:1:12805)     在f.r.emit(https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:1:19393)     在XMLHttpRequest.n.onreadystatechange(https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js:1:16712)     在XMLHttpRequest.wrapFn [as _onreadystatechange](http://localhost:4200/vendor.bundle.js:158112:29)     在ZoneDelegate.invokeTask(http://localhost:4200/vendor.bundle.js:157498:35)     在Object.onInvokeTask(http://localhost:4200/vendor.bundle.js:39616:37

  • 加入
  

received.affectedChannels.forEach((channel)=> {

此部分出现无法读取未定义属性'forEach'的错误

import config from '../config.json';

class Mock {

  constructor(broadcaster) {
    this.listener = null;
    this.broadcaster = broadcaster;
    this.broadcastChannels = {};
  }

  /**
   * Initialize the listener for broadcasting all events
   *
   * @param {wrapper} instance
   */
  initializeListener(instance) {
    if (this.listener === null) {
      let self = this;

      self.listener = {};

      config.subscribe_listener_events_to_broadcast.forEach((event) => {
        self.listener[event] = function (received) {
          if (received.subscription && self.broadcastChannels[received.subscription] && self.broadcastChannels[received.subscription].includes(event)) {
            self.broadcaster.emit(event, received.subscription, received);

            if (received.channel) {
              self.broadcaster.emit(event, received.channel, received);
            }
          }

          if (received.channel && self.broadcastChannels[received.channel] && self.broadcastChannels[received.channel].includes(event)) {
            self.broadcaster.emit(event, received.channel, received);
          }

          if (event === 'status') {
            if (received.error) {
              self.broadcaster.emitError(received);
            } else {
              received.affectedChannels.forEach((channel) => {
                if (self.broadcastChannels[channel] && self.broadcastChannels[channel].includes(event)) {
                  self.broadcaster.emit(event, channel, received);
                }
              });

              received.affectedChannelGroups.forEach((channelGroup) => {
                if (self.broadcastChannels[channelGroup] && self.broadcastChannels[channelGroup].includes(event)) {
                  self.broadcaster.emit(event, channelGroup, received);
                }
              });
            }
          }
        };
      });

      instance.getOriginalInstance().addListener(this.listener);
    }
  }

  /**
   * Adds a set of channels to all events to broadcast
   *
   * @param {[string]} channels
   * @param {true|['message', 'presence', 'status']} triggerEvents
   */
  addEventsBroadcast(channels, triggerEvents) {
    channels.forEach((channel) => {
      if (typeof triggerEvents === 'boolean') {
        this.broadcastChannels[channel] = config.subscribe_listener_events_to_broadcast;
      } else if (Array.isArray(triggerEvents)) {
        this.broadcastChannels[channel] = [];

        triggerEvents.forEach((trigger) => {
          if (config.subscribe_listener_events_to_broadcast.includes(trigger)) {
            this.broadcastChannels[channel].push(trigger);
          }
        });
      }
    });
  }

  /**
   * Removes a set of channels from of all events to broadcast
   *
   * @param {[string]} channels
   */
  removeEventBroadcast(channels) {
    channels.forEach((channel) => {
      if (this.broadcastChannels[channel]) {
        delete this.broadcastChannels[channel];
      }
    });
  }

  /**
   * Enable a set of channels or group of channels to the broadcaster
   *
   * @param {object} args
   */
  enableEventsBroadcast(args) {
    if (args.channels) {
      this.addEventsBroadcast(args.channels, args.triggerEvents);
    }

    if (args.channelGroups) {
      this.addEventsBroadcast(args.channelGroups, args.triggerEvents);
    }
  }

  /**
   * Disable a set of channels or group of channels from the broadcaster
   *
   * @param {object} args
   */
  disableEventsBroadcast(args) {
    if (args.channels) {
      this.removeEventBroadcast(args.channels);
    }

    if (args.channelGroups) {
      this.removeEventBroadcast(args.channelGroups);
    }
  }
}

module.exports = Mock;

module.exports.Mock = Mock;



//////////////////
// WEBPACK FOOTER
// ./~/pubnub-angular2/src/mock.js
// module id = 1075
// module chunks = 3

pubnub代码

pubnubService.init({
      publishKey: 'pub-c-b7a9da53-6623-475e-a0ba-c507a6bd1b1d',
      subscribeKey: 'sub-c-0956782e-e91e-11e6-8919-0619f8945a4f'
    });

    pubnubService.subscribe({
      channels: ['ADMIN-' + this.branchIdSave],
      withPresence: true,
      triggerEvents: ['message', 'presence', 'status']
    });

    pubnubService.getMessage('ADMIN-' + this.branchIdSave, function (msg) {      
      self.getBadge(msg);
    });
  }

  getBadge(msg) {
    this.newOrderCount = msg['message'].branchBadge['newOrderCount'];
    this.preservedOrderCount = msg['message'].branchBadge['preservedOrderCount'];
    this.waitingApplicantCount = msg['message'].branchBadge['waitingApplicantCount'];

    console.log(msg);
  }

0 个答案:

没有答案