Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
- import { Observable } from 'rxjs/Observable';
- import { User } from './user';
- @Injectable()
- export class FirestoreDataService {
- userscollection: AngularFirestoreCollection<User>;
- users: Observable<User[]>;
- userDoc: AngularFirestoreDocument<User>;
- constructor(public _afs: AngularFirestore) {
- //this.users = this._afs.collection('Users').valueChanges();
- this.userscollection = this._afs.collection('Users', x => x.orderBy('firstname', 'asc'));
- this.users = this.userscollection.snapshotChanges().map(
- changes => {
- return changes.map(
- a => {
- const data = a.payload.doc.data() as User;
- data.id = a.payload.doc.id;
- return data;
- });
- });
- }
- getUsers() {
- return this.users;
- }
- addUser(user) {
- this.userscollection.add(user);
- }
- deleteUser(user) {
- this.userDoc = this._afs.doc(`Users/${user.id}`);
- this.userDoc.delete();
- }
- }
Add Comment
Please, Sign In to add comment