热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

我有一个聊天设计,其中的左侧有一个人列表,单击任何人,我都会在右侧打开

我有一个聊天设计,其中的左侧有一个人列表,单击任何人,我都会在右侧打开他的聊天记录。因此,我需要使用RXJS订阅调用将用户详细信息从一个组件发送到另一个组件。现在,数据正在成功传输,但是问题是,在正确的组件中通过subscribe调用接收到用户详细信息之后,我正在调用一个API,以使用户能够进行聊天记录,并希望在该组件中显示聊天记录,为此,我需要在API的成功响应中添加for循环,但是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 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
);
}
});
}
`

我成功获取了'chat data res'控制台,但是在for循环中,我得到了'message response'未定义,并且以下consol未被执行。请为我建议解决方案或其他选择。


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

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

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

在上方检查,您将获取数据,并且在进行此更改之后,将res[i]['user_profile_id']替换为res.data[i]['user_profile_id']。在需要的地方对您的代码进行更改


推荐阅读
author-avatar
大美妞meilei
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有