无法解析JSON数据与rxjs的接口

时间:2018-06-17 13:42:40

标签: json typescript interface rxjs

我有一个webSocket,我从服务器获取数据。我创建了一个返回数据的rx Subject MessageEvent。

但是,在我的observable上,我可以控制日志我的对象并打印json数据,但我无法访问任何属性,它返回undefined。

export interface Message {
request: string;
subscription: string,
}

@Injectable()
export class SocketProvider {

public message: Subject<Message>;

constructor(private socket: WebsocketProvider) {
  console.log('Hello SocketProvider Provider');
  this.message = <Subject<Message>>socket
  .connect('ws://192.168.0.110:8081')
  .map((response: MessageEvent): Message => {
    let data = response.data;
    return data;
  })
}

}

和用法:

ionViewDidEnter() {
    console.log('ionViewDidEnter ConfigsInputsPage');
      this.subscribtion =  this.socket.message.subscribe(msg => {
      this.message = msg;
      console.log("Response From webSocket server: " + msg);
      console.log(msg.request);
    });
    this.socket.message.next(this.enterMessage);
}

private enterMessage = { 
  request: 'configs',
  subscription: 'suscribe'
}

1 个答案:

答案 0 :(得分:0)

好的。我添加以再次在socket.ts中解析json的事实:

constructor(private socket: WebsocketProvider) {
  console.log('Hello SocketProvider Provider');
  this.message = <Subject<Message>>socket
  .connect('ws://192.168.0.110:8081')
  .map((response: MessageEvent): Message => {
      let data = JSON.parse(response.data); //  <----------
      return data;
  })
}