7

I have two fire store collection with following reference image .enter image description hereenter image description here I want to get the firstName and title. Here signup_id is referenced from coll-signup document id. Following code given below what i did.

Model feed.ts

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

news feed.component template

    <ul *ngFor="let feed of feeds">
<!-- <li>{{feed.firstName}}</li> --> // Here I want to print my first name
      <li>{{feed.title}}</li>
      <li>{{feed.signup_id}}</li>
    </ul>

news-feed.component.ts

import { Component, OnInit } from '@angular/core';
import { Feed } from '../../models/feed';
import { FeedService } from '../../services/feed.service';
@Component({
  selector: 'app-news-feed',
  templateUrl: './news-feed.component.html',
  styleUrls: ['./news-feed.component.css']
})
export class NewsFeedComponent implements OnInit {
  feeds : Feed[];
  constructor(private feedService: FeedService) { }

  ngOnInit() {
    this.feedService.sellectAllNews().subscribe(feeds => {
      this.feeds = feeds;
    })
  }

}

feed.service.ts

    import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
    import { Observable } from 'rxjs/Observable';
    import { Feed } from '../models/feed';
    @Injectable()
    export class FeedService {
      feedCollection : AngularFirestoreCollection<Feed>;
      feedItem : Observable<Feed[]>;
    
      constructor(private afs : AngularFirestore) { 
        this.collectionInitialization();
      }
    
      collectionInitialization() {
// I think here I have to modify or add next collection to will get the output
        this.feedCollection = this.afs.collection('col-challange');
        this.feedItem = this.feedCollection.stateChanges().map(changes => {
          return changes.map(a => {
            const data = a.payload.doc.data() as Feed;
            return data;
          })
        })
    
      }
      sellectAllNews() {
        this.collectionInitialization();
        return this.feedItem;
      }
    }

this is possible to do fire store? I'm newbie in fire store.Please help me. Thanks!

Community
  • 1
  • 1
Rijo
  • 2,963
  • 5
  • 35
  • 62

2 Answers2

15

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));
Hareesh
  • 6,770
  • 4
  • 33
  • 60
  • When I import 'rxjs/add/operator/mergeMap'; flatMap error cleared. Should I import any other observable? – Rijo Nov 17 '17 at 01:25
  • import `import { Observable } from 'rxjs/Observable';` – Hareesh Nov 17 '17 at 12:46
  • that is the only import i used. if nothing works try to import all and check `import 'rxjs/Rx'; ` – Hareesh Nov 17 '17 at 12:50
  • `console.log(data); console.log(signup);` output `{signup_id: "NxQDrGzF0Qxhni94vvCq", title: "new chalange"} {firstName: "Amal", mob: "7891234562"}` for this `return { signup_id: signup.firstName, ...data };` getting output only `signup_id` and `title` for this `return { signup_id: signup.firstName, data };` getting only `firstName` from my html template. How this join together to print? – Rijo Nov 18 '17 at 04:08
  • i didn't get the above comment, try printing json in html and check the data is correct. `
    • {{feed | json}}
    `
    – Hareesh Nov 18 '17 at 04:39
  • `
    • {{feed.firstName}}
    • {{feed.title}}
    • {{feed.signup_id}}
    ` Its my template format. For your given code I'm getting only `title' and `signup_id`. I didn't get `firstName`.
    – Rijo Nov 18 '17 at 06:00
  • my mistake it should be `return { firstName: signup.firstName, ...data };` – Hareesh Nov 18 '17 at 08:22
  • Hi Thank you so much. Can u please refer some tutorials for complicated query handling like this. One more thing what is the meaning of `...data`? How it will work here? – Rijo Nov 18 '17 at 08:55
  • that is a feature of `ES6`(javascript) check this [link](https://odetocode.com/blogs/scott/archive/2014/09/02/features-of-es6-part-5-the-spread.aspx) – Hareesh Nov 18 '17 at 08:58
8

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);
});
skaveesh
  • 369
  • 6
  • 9