我遇到了一个问题:我首先需要获取评论数据(针对用户),其次需要获取用户数据并将评论与用户相关联:
结构数据:
comment:
id,
date,
userId,
message
--------
user:
id,
name
我想获取评论,并为每条获得评论添加用户名。即我想让评论包含用户名:
comment:
id,
date,
userId,
message,
userName
我阅读了rxjs的文档,但是由于各种运算符的丰富,我不知道什么适合我。
我尝试
//get comments
return this.getMovieComments(movieId).pipe(
//form array ids users by comments
mergeMap(comments => {
const ids = comments.map(c => c.userId);
//get users
return this.userService.searchUsers({ id: ids });
})
).subscribe((users) => { console.log(users) });
这是可行的,但是我只是获得列表用户,不知道如何关联getMovieComments()和searchUsers()的数据,然后获得所需的结果。
很抱歉,那是显而易见的事情。我是RxJS的初学者
在此行
return this.userService.searchUsers({ id: ids });
您可以将其与comments
(您已经拥有)结合使用:
return forkJoin(of(comments), this.userService.searchUsers({ id: ids }));