Advertisement
Guest User

Untitled

a guest
Jun 29th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
  3. import { Observable } from 'rxjs';
  4. import { map } from 'rxjs/operators';
  5.  
  6. @Injectable({
  7.   providedIn: 'root'
  8. })
  9. export class FirebaseService {
  10.  
  11.   private recordsCollection: AngularFirestoreCollection<any[]>;
  12.   public records$: Observable<any[]>;
  13.  
  14.   constructor(private afs: AngularFirestore) {
  15.     this.recordsCollection = this.afs.collection<any[]>('records');
  16.     this.records$ = this.getDocs(this.recordsCollection);
  17.   }
  18.  
  19.   getDocs(collection: AngularFirestoreCollection<any[]>): any {
  20.     return collection.snapshotChanges()
  21.       .pipe(
  22.         map(actions => actions.map(a => {
  23.           const data = a.payload.doc.data();
  24.           const id = a.payload.doc.id;
  25.           return { id, ...data };
  26.         }))
  27.       );
  28.   }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement