无法从构造函数的回调中访问字典属性

时间:2016-05-18 06:42:34

标签: javascript dictionary typescript

我已经使用https://github.com/basarat/typescript-collections#a-sample-on-dictionary创建了一个字典并为其插入了值,如下所示。

private subscribedTopicsDict = new collections.Dictionary<string, MyTopic>();
 this.subscribedTopicsDict.setValue(topic, myTopic);

当我打印键的值时,&#34; myTopic&#34;设置后立即打印出来。但是当我在构造函数中的回调中调用它时,什么也没发生。 getValue行执行后没有错误但没有任何错误。以下是我的称呼方式。

constructor(url: string, mqttOptions: MqttOptions, messageReceivedCallBack: IMessageReceivedCallBack) {
    if (!_.isString(url) || _.isEmpty(mqttOptions || _.isEmpty(messageReceivedCallBack))) {
      throw new MyError('invalid url value');
    } else {
      this.client = mqtt.connect(url, mqttOptions);
      this.client.on('message', (topic: string, message: string) => {
        let myMessage: myMessage;
        let receivedTopic: myTopic;
        console.log('1.onMessageReceived called with topic : ' + topic);
        receivedTopic = this.subscribedTopicsDict.getValue(topic);
        console.log('2.received topic and dataTypeID' + receivedTopic.dataTypeID);
        myMessage = {
          myTopic: receivedTopic,
          payload: message
        };
        console.log('about to fire the callback');
        messageReceivedCallBack.onMessageReceived(myMessage);
      });
    }

  }

第二个控制台日志未打印。我尝试使用脚本类型的默认字典功能,并获得相同的行为。

0 个答案:

没有答案
相关问题