For循环未在订阅事件中执行

无名氏

我有一个聊天设计,其中的左侧有一个人列表,单击任何人,我都会在右侧打开他的聊天记录。因此,我需要使用RXJS订阅调用将用户详细信息从一个组件发送到另一个组件。现在,数据正在成功传输,但是问题是,在通过subscribe正确的组件中的调用接收到用户详细信息之后,我正在调用一个API来使用户聊天记录,并希望在该组件中显示聊天记录,因此,我需要添加for循环API的成功响应,但for循环未执行。下面是我的右侧组件代码。

` ngOnInit() {

    this.userProfileId = this.utilService.getCurrentLoginUserProfileId();
    if (this.userProfileId) {

      this.friendId = this.userDetail.userProfileId;
       // get users data
      this.utilService.onChangeUserDetailData.subscribe(data => {
      console.log('data', data);
      if (data) {
        this.userDetail = data;
        this.friendId = this.userDetail.userProfileId;
        this.sendUserOnlineSocket(data);


        // get chat history api
        this.getUsersChatData(data);
        }
    });

    }

// get users chat data
  getUsersChatData(user: any) {
    let postObj;
    if (user.chatRoomId) {
      postObj = {
        from_user: this.userProfileId,
        to_user: user.chatRoomId
      };
    } else if (user.userProfileId) {
      postObj = {
        from_user: this.userProfileId,
        to_user: user.userProfileId
      };
    }

    this.chatPageService.getChatData(postObj).then((res: any) => {
      console.log(' chat data res', res);
      if (res['status'] === 200) {
        if (res['data']['length'] > 0) {
          for (let i = 0; i < res.data.length; i++) {
            console.log('message response', res[i]);
            if (res[i]['user_profile_id'] === this.userProfileId && res[i]['to_user_id'] === this.friendId) {
              this.messageData.push({ side: 'right side', data: res[i] });
            } else {
              this.messageData.push({ side: 'left side', data: res[i] });
            }
          }
          console.log('message data', this.messageData);
        }
        console.log('message data', this.messageData);
      }
      console.log('message data', this.messageData);
    }).catch(err => {
          if (err.hasOwnProperty('error')) {
            this.utilService.errorHandler(err.error);
          } else {
            this.utilService.showError(
              'Error',
              this.utilService.commonErrorMsg
            );
          }
    });
  }

`

我已成功获取“聊天数据资源”控制台,但在for循环中,我未定义“消息响应”,并且未执行以下控制台。请为我提出解决方案或其他选择。

马姆塔

根据您的代码,您正在犯一个错误。

而不是res[i]你需要检查res.data[i]

for (let i = 0; i < res.data.length; i++) {
        console.log('message response', res.data[i]);             
 }

选中上面的复选框,您将获取数据,更改之后,请替换res[i]['user_profile_id']res.data[i]['user_profile_id']随时随地对您的代码进行更改

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

For循环未在量角器中执行

来自分类Dev

循环未在函数C ++中执行

来自分类Dev

内部For循环未在App脚本中执行

来自分类Dev

For循环未在量角器中执行

来自分类Dev

路由器事件未在单元测试中执行

来自分类Dev

如何订阅PencilBlue中的事件?

来自分类Dev

Java中的事件订阅模式

来自分类Dev

动态订阅方法中的事件?

来自分类Dev

Click事件未在<object>标记上执行

来自分类Dev

AngularCli - 测试期间未在 onSubmit 内执行订阅

来自分类Dev

JavaScript事件循环乱序执行

来自分类Dev

jQuery Ajax调用未在for循环中执行

来自分类Dev

jQuery Ajax调用未在for循环中执行

来自分类Dev

排序未在jQuery中执行

来自分类Dev

线程未在C中执行

来自分类Dev

函数未在脚本中执行

来自分类Dev

退出未在脚本中执行

来自分类Dev

脚本未在SQL中执行

来自分类Dev

if 语句未在 Ruby 中执行

来自分类Dev

while循环未在python中循环

来自分类Dev

durandaljs中的已订阅事件并非未订阅

来自分类Dev

在脚本执行Powershell之后继续事件订阅

来自分类Dev

Tcl中的事件循环

来自分类Dev

Moq不订阅构造函数中的事件

来自分类Dev

在Prism的其他模块中订阅事件

来自分类Dev

如何清除C ++ / CLI中的事件订阅?

来自分类Dev

在Prism的其他模块中订阅事件

来自分类Dev

取消订阅页面更改中的事件

来自分类Dev

如何取消订阅 TornadoFX 中的事件?