在控制台上记录整个响应显示数据。但是当我尝试在打字稿中获取个人 属性 时,它给出了未定义的。为什么?

Logging entire response on console show the data. But when i try to get the individual property in typescript it gives undefined. Why?

我在 angular 中捕获 socket.io 事件时收到一些响应。当我在控制台上记录整个响应时,它会显示所有属性,但是当我尝试获取单个 属性 时,它会给出未定义的。我附上一张照片以供参考。 P.S: 我是脚本新手。

this._chatService.onNewUserJoined()
  .subscribe((data:any) => {
    console.log(data);
    console.log(data['userName']);
    this.snackBar.open(`${data['userName']} Joined chat room!!!`, "ok", {
      duration: 2000,
    });
  });

谢谢enter image description here

您的回复是array,您需要使用索引或with循环访问,下面应该可以正常工作。

 console.log(data);
 console.log(data[0]['userName']);