如何以正确的方式从Firestore检索数据

时间:2019-07-27 16:18:10

标签: angular rxjs google-cloud-firestore

我想从Firestore后端检索数据,但是访问数据时遇到问题。我的文件如下所示:

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$)
  }

}

要初始化配置文件组件时,我想从可观察的用户$中访问数据,但是我不知道该怎么做...任何人都可以帮我吗?

0 个答案:

没有答案