I am trying to combine few observables and passing the value from first one through the entire pipe.
我正在尝试组合几个可观测的变量,并将第一个变量的值传递到整个管道。
this.auth.user
.pipe(
switchMap((authUser) => {
if (!authUser) {
return of();
}
const docRef = doc(this.firestore, firebaseCollections.UsersCollection, authUser.uid);
return from(getDoc(docRef));
}),
map((user)=>{
if(user){
return true;
}
return false
}),
switchMap([authUser, exists])={
}
)
On the last switchMap I would like to get the authUser
from the from the first **switchMap ** but also the value from the map. I know I've done it in the past put can't find the solution anymore.
在最后一个SwitchMap上,我希望从第一个**SwitchMap**中获取authUser,同时也从map中获取值。我知道我已经做过了,现在已经找不到解决的办法了。
I guess it should be something like this but not sure witch rxjs functions should I use:
我猜应该是这样的,但不确定我应该使用哪个rxjs函数:
1: (authUser) => getFirestoreUser(authUser)
1:(AuthUser)=>getFirestore User(AuthUser)
2: (authUser, firestoreUser) => {...}
2:(authUser,FireStoreUser)=>{...}
更多回答
优秀答案推荐
I'm not a expertice in rxjs, but I feel you need "map" your intermediate response. Some like
我不是rxjs方面的专家,但我觉得您需要“映射”您的中间响应。有些人喜欢
this.auth.user
.pipe(
switchMap((authUser) => {
if (!authUser) {
return of([null,null] as [any,any]);
}
const docRef = doc(this.firestore, firebaseCollections.UsersCollection, authUser.uid);
//see that you use here pipe(map) to return an array
return from(getDoc(docRef)).pipe(
map((x:any)=>[x,authUser] as [any,any])
);
}),
map([user,authUser]:[any,any])=>{
if(user){
return [authUser,true] as [any,boolean];
}
return [authUser,false] as [any,boolean]
}),
switchMap([authUser, exists]:[any,boolean])={
}
)
NOTE: I use the "as [any:any]" to keep the type of the users
注意:我使用“as[any:any]”来保持用户的类型
更多回答
我是一名优秀的程序员,十分优秀!