Guest User

Untitled

a guest
Dec 11th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
  3.  
  4. import { Observable } from 'rxjs/Observable';
  5. import { User } from './user';
  6. @Injectable()
  7. export class FirestoreDataService {
  8. userscollection: AngularFirestoreCollection<User>;
  9. users: Observable<User[]>;
  10. userDoc: AngularFirestoreDocument<User>;
  11. constructor(public _afs: AngularFirestore) {
  12. //this.users = this._afs.collection('Users').valueChanges();
  13.  
  14. this.userscollection = this._afs.collection('Users', x => x.orderBy('firstname', 'asc'));
  15. this.users = this.userscollection.snapshotChanges().map(
  16. changes => {
  17. return changes.map(
  18. a => {
  19. const data = a.payload.doc.data() as User;
  20. data.id = a.payload.doc.id;
  21. return data;
  22. });
  23.  
  24. });
  25.  
  26. }
  27. getUsers() {
  28. return this.users;
  29. }
  30. addUser(user) {
  31. this.userscollection.add(user);
  32. }
  33. deleteUser(user) {
  34. this.userDoc = this._afs.doc(`Users/${user.id}`);
  35. this.userDoc.delete();
  36. }
  37. }
Add Comment
Please, Sign In to add comment