Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何链接rxjs观测值以获取内部观测值,并在角度上与父级一起返回_Angular_Firebase_Google Cloud Firestore_Rxjs_Observable - Fatal编程技术网

Angular 如何链接rxjs观测值以获取内部观测值,并在角度上与父级一起返回

Angular 如何链接rxjs观测值以获取内部观测值,并在角度上与父级一起返回,angular,firebase,google-cloud-firestore,rxjs,observable,Angular,Firebase,Google Cloud Firestore,Rxjs,Observable,我向firestore发出请求以获取用户的聊天记录,它返回一个具有此形状的可观察对象数组 [..., { key: '83hed87djhe09', participants: ['owner_43', 'visitor_69'] }, ...] 这将显示UI中所有用户聊天的列表,但是我想按用户名搜索聊天。为了做到这一点,我必须向后端服务器发出http请求,请求每个参与者的用户名,并在参与者线程中替换该用户名,以使其在类型上可搜索 例如,“owner_43”就变成了

我向firestore发出请求以获取用户的聊天记录,它返回一个具有此形状的可观察对象数组

[...,
  {
     key: '83hed87djhe09',
     participants: ['owner_43', 'visitor_69']
  },
...]
这将显示UI中所有用户聊天的列表,但是我想按用户名搜索聊天。为了做到这一点,我必须向后端服务器发出http请求,请求每个参与者的用户名,并在参与者线程中替换该用户名,以使其在类型上可搜索

例如,“owner_43”就变成了“John Doe”

我遇到的问题是,我得到的是参与者名称的可观察数组,而不是字符串数组

这是我的密码

this.chat$=this.chatSvc.getUserChats(this.userUID).pipe(
地图((聊天室:任何)=>{
返回chats.map((chat:any)=>{
返回{
key:chat.key,
参与者:chat.participants.map((参与者:任意)=>{
返回此.userSvc.getUserFromString(参与者);
})
}
})
})
);
下面是getUserFromString函数:

getUserFromString(stringId){
设splitValue=stringId.split(“”);
让accountType=splitValue[0];
设id=splitValue[1];
如果(accountType==‘所有者’){
返回此.ownerSvc.getOwner(id);
}
else if(accountType==='visitor'){
返回此.visitorSvc.getVisitor(id);
}
}
Get owner函数只返回:

返回这个.http.get(owner\u url+id);
最后,使用角度异步管道在视图中展开结果

    {{msg.key}

我做错了什么?

假设您的
聊天记录是这样的:

/**
*正在使用类型参数
*由于参与者最初为string[]类型
*最终为用户[]类型
*/
课堂聊天{
键:字符串;
与会者:T[]
}
考虑以下实现:

this.chat$:Observable=this.chatSvc.getUserChats(this.userUID).pipe(
mergeAll(),
合并地图({key,participants}:Chat)=>{
返回forkJoin(participants.map(this.userSvc.getUserFromString)).pipe(
映射(参与者=>({key,participants}))
)
}),
toArray()
)
解释(简化):

this.chat$=this.chatSvc.getUserChats(this.userUID).pipe(
/**
*将可观察到的物体压平成一条小溪
*我们可以一次处理一次聊天
*/
mergeAll(),
/**
*将每次聊天转换为
*包含新值的可观察的
*/
合并地图({key,participants}:Chat)=>{
/**
*将参与者(字符串[])转换为可观察的数组
*(可观察[])
*/
const participants$=participants.map(this.userSvc.getUserFromString)
/**
*使用forkJoin等待所有可观察对象完成
*然后使用map返回新聊天
*/
返回forkJoin(参与者)。管道(
映射(参与者=>({key:key,参与者:参与者}))
)
}),

toArray()//谢谢您的回复,我尝试实现了这个,但它不断返回错误:无法读取getUserFromString中未定义的属性'ownerSvc'。似乎与'this'关键字有关,您对如何解决它有什么想法吗?已修复。(
particients.map(x=>this.userSvc.getUserFromString(x))
而不是解决问题的
participants.map(this.userSvc.getUserFromString)
!解决方案一直工作到toArray(),即它对用户数据进行查询并将结果设置为
{key:'key_id',参与者:[{participant_object_1},{participant_object_2}]
对于每条消息,但不返回所有消息sis
getUserChats
Subject或HttpClient请求?还可以尝试使用
first()
操作符,该操作符位于
mergeAll