Firestore how to get the collection value from another collection document id is referenced

前端 未结 2 1329
礼貌的吻别
礼貌的吻别 2020-12-03 06:23

I have two fire store collection with following reference image . I want to get the firstName and title. Here signup_id is referenced from col

相关标签:
2条回答
  • 2020-12-03 06:39

    For anyone experiencing issues, combining documents in Firebase Cloud Firestore while using Angular6, RxJS 6 and AngularFire v5.0 try the following code

    Model feed.ts

    export interface Feed {
      firstName?: string;
      signup_id?: string;
      title?: string;
    }
    
    export interface CollSignup {
      firstName: string;
      mob: string;
    }
    
    export interface ColChallange {
      signup_id: string;
      title: string;
    }
    

    Service feed.service.ts

    import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
    import { Observable, combineLatest } from 'rxjs';
    import {flatMap, map} from 'rxjs/operators';
    import {ColChallange, CollSignup, Feed} from '../../models/feed';
    
    @Injectable()
    export class FeedService {
      colChallangeCollection: AngularFirestoreCollection<ColChallange>;
      feedItem: Observable<Feed[]>;
    
      constructor(private afs: AngularFirestore) { }
    
      collectionInitialization() {
        this.colChallangeCollection = this.afs.collection('col-challange');
         this.feedItem = this.colChallangeCollection.snapshotChanges().pipe(map(changes  => {
          return changes.map( change => {
            const data = change.payload.doc.data();
            const signupId = data.signup_id;
            const title = data.title;
              return this.afs.doc('coll-signup/' + signupId).valueChanges().pipe(map( (collSignupData: CollSignup) => {
                return Object.assign(
                  {firstName: collSignupData.firstName, signup_id: signupId, title: title}); }
              ));
          });
        }), flatMap(feeds => combineLatest(feeds)));
      }
    
      sellectAllNews() {
        this.collectionInitialization();
        return this.feedItem;
      }
    }
    

    To print all the data

    this.feedItem.forEach(value => {
      console.log(value);
    });
    
    0 讨论(0)
  • 2020-12-03 06:42

    You need to combine two collections.

    try this code

    this.feedCollection = this.afs.collection('col-challange');
    this.feedItem = this.feedCollection.snapshotChanges().map(changes => {
          return changes.map(a => {
            //here you get the data without first name
            const data = a.payload.doc.data() as Feed;
            //get the signup_id for getting doc from coll-signup
            const signupId = data.signup_id;
            //get the related document
            return afs.collection('coll-signup').doc(signupId).snapshotChanges().take(1).map(actions => {
              return actions.payload.data();
            }).map(signup => {
              //export the data in feeds interface format
              return { firstName: signup.firstName, ...data };
            });
          })
        }).flatMap(feeds => Observable.combineLatest(feeds));
    
    0 讨论(0)
提交回复
热议问题