0

I want to retrieve data from my Firestore backend, but I have a problem accessing the data. My files look like this:

data.service.ts

import { Injectable } from '@angular/core';

import {AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument} from '@angular/fire/firestore';
import { HttpClient } from '@angular/common/http';
import { Incident } from '../profile/profile.component';
import { User } from '../../model/user';
import {Observable} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class DataService {

  user$: Observable<User>;

  constructor(private http: HttpClient, private angularFirestore: AngularFirestore) { }

  fetchIncidentsFromUser(uid: string) {
  }

  reportIncident(newIncident: Incident) {
  }

  fetchUserData(uId: string): void {
    const user: AngularFirestoreDocument<User> = this.angularFirestore.doc('users/' + uId);
    this.user$ = user.valueChanges();
  }

  getUserData(): Observable<User> {
    return this.user$;
  }



}

profile.component.ts

import { Component, OnInit } from '@angular/core';
import {DataService} from '../services/data.service';
import {User} from '../../model/user';
import {Observable} from 'rxjs';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {

  user$: Observable<User>;

  constructor(private dataService: DataService) {

  }

  ngOnInit() {
    this.user$ = this.dataService.getUserData();
    console.log(this.user$)
  }

}

When the profile component is going to be initialized, I want to access the data from the observable user$, but I don't know how to do that...can anyone help me?

Andreas Teich
  • 349
  • 1
  • 2
  • 11
  • Well nowhere are you actually calling `fetchUserData`?? – AT82 Jul 27 '19 at 18:54
  • 1
    So you're asking how to get data from an observable? Then see this question [RxJs get value from observable](https://stackoverflow.com/q/37607257/9423231). – frido Jul 27 '19 at 20:20

0 Answers0