Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
- import { Observable } from 'rxjs';
- import { map } from 'rxjs/operators';
- @Injectable({
- providedIn: 'root'
- })
- export class FirebaseService {
- private recordsCollection: AngularFirestoreCollection<any[]>;
- public records$: Observable<any[]>;
- constructor(private afs: AngularFirestore) {
- this.recordsCollection = this.afs.collection<any[]>('records');
- this.records$ = this.getDocs(this.recordsCollection);
- }
- getDocs(collection: AngularFirestoreCollection<any[]>): any {
- return collection.snapshotChanges()
- .pipe(
- map(actions => actions.map(a => {
- const data = a.payload.doc.data();
- const id = a.payload.doc.id;
- return { id, ...data };
- }))
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement