简体   繁体   English

如何获取可观察的 <Array[]> 从angularfire2中的引用数组?

[英]How to retrieve Observable<Array[]> from array of references in angularfire2?

Let's say I have a following structure, that means that user have and array of objects containing references to groups. 假设我具有以下结构,这意味着用户拥有包含对象组引用的对象和数组。 I need to get a single observable for an array of referenced groups. 我需要为一组参考组获取一个可观察的对象。 How do I do that? 我怎么做? I've tried a lot of different ways but no one is correct. 我尝试了很多不同的方法,但是没有人是正确的。

在此处输入图片说明

Here's what I came up with so far: 到目前为止,这是我想到的:

 export interface Group { students: DocumentReference[]; title: string; } @Injectable() export class GroupsService { constructor(private afs: AngularFirestore, private tutorsService: TutorsService) { } getCurrentTutorGroups(): Observable<Group[]> { return this.tutorsService.tutor.pipe( map(tutor => { return tutor.groupSubjects.map(groupSubject => groupSubject.group.get()); }), mergeMap(group => fromPromise(group)), map(group => group.data() as Group) ); } } 

  GroupSubjects { group: DocumentReference; subject: DocumentReference; } interface Tutor extends User { groupSubjects: GroupSubjects[]; } @Injectable() export class TutorsService { tutor: Observable<Tutor>; constructor(private afs: AngularFirestore, private authService: AuthService) { this.tutor = this.authService.user.map((user: User) => { return {...user} as Tutor; }); } } 

 export interface User { uid: string; email: string; name: string; role: string; } @Injectable() export class AuthService { user: Observable<User | null>; constructor(private afAuth: AngularFireAuth, private afs: AngularFirestore) { this.setUser(); } private setUser() { this.user = this.afAuth.authState .switchMap((user) => { if (user) { return this.afs.doc<User>(`users/${user.uid}`).snapshotChanges().map(action => { return {uid: action.payload.id, ...action.payload.data()}; }); } else { return Observable.of(null); } }); } } 

I need function getCurrentTutorGroups to return observable of type Group[]. 我需要函数getCurrentTutorGroups返回可观察的Group []类型。

Here's my solution after all the struggle 经过所有的努力,这是我的解决方案

 import { Injectable } from '@angular/core'; import {AngularFirestore} from 'angularfire2/firestore'; import {Observable} from 'rxjs/rx'; import 'rxjs/add/operator/mergeMap'; import 'rxjs/add/operator/map'; import { firestore} from 'firebase'; import DocumentReference = firestore.DocumentReference; import {GroupSubjects, TutorsService} from './tutors.service'; import {fromPromise} from 'rxjs/observable/fromPromise'; export interface Group { students: DocumentReference[]; title: string; } @Injectable() export class GroupsService { constructor(private afs: AngularFirestore, private tutorsService: TutorsService) { } private getTutorGroupsObservableArray(groupSubjects: GroupSubjects[]): Observable<Group>[] { return groupSubjects.map(groupSubject => fromPromise<Group>(groupSubject.group.get().then(group => group.data() as Group))); } getCurrentTutorGroups(): Observable<Group[]> { return this.tutorsService.tutor.map(tutor => Observable.combineLatest(this.getTutorGroupsObservableArray(tutor.groupSubjects))).mergeMap(group => group); } } 

Remember to add 记住要添加

 import {Observable} from 'rxjs/rx'; 

instead of 代替

 import {Observable} from 'rxjs/Observable'; 

And one more thing that helped me finding the way to do that is that I added my private method for this code to be more obvious. 还有另一件事帮助我找到了实现此目标的方法,就是为该代码添加了我的私有方法,以使其更加明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM