Angular Firestore 如何检索子集合文档进行编辑?

Mic*_*nos 3 angular google-cloud-firestore

我正在尝试从 firestore 子集合中检索单个文档:database/users/uid/animal/docID

我能够成功地从另一个组件解析出 docID,但我正在努力检索要显示在 html 中的信息:

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../core/auth.service';
import { AngularFireAuth} from 'angularfire2/auth';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import { Router, ActivatedRoute } from '@angular/router';

interface Animal {
 name: string;
 age: number;
 sex: string;
 breed: string;
 colour: string;
 }

 interface animID extends Animal {
   id: string;
 }

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

 curUser: any; // This used to maintain the logged in user. 
 animalDoc: AngularFirestoreDocument<Animal>;
 animalCol: AngularFirestoreCollection<Animal>;
 animalInfo: any;
 petID: Observable<Animal>;

 constructor(
  public auth: AuthService, 
  private afs: AngularFirestore, 
  private afAuth: AngularFireAuth, 
  private router: Router,
  public route: ActivatedRoute
  ) {
  const petID: string = route.snapshot.paramMap.get('id'); 
  console.log('AnimId from route: ', petID)
  const user: any = this.afAuth.authState
 }
 private curPetID: string = this.route.snapshot.paramMap.get('id');

 ngOnInit() {
  this.afAuth.auth.onAuthStateChanged((user) => {

  if (user) {
    // get the current user    
    this.curUser = user.uid;
    console.log('Animal ID:', this.curPetID )
    console.log('Current User: ', this.curUser);
    // Specify the Collection
    this.animalInfo = this.afs.collection(`users/${this.curUser}/animals/`, 
    ref => ref.where('id', "==", this.curPetID)
        .limit(1))
        .valueChanges()
        .flatMap(result => result)
        console.log('Got Docs:', this.animalInfo);
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的 HTML 中(现在只显示它):

<strong>Name: {{ (animalInfo | async)?.name }}</strong>
<br>Breed: {{ (animalInfo | async)?.breed }}
<br>Animal System ID: {{ (animalInfo | async)?.id }}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,在 console.log('GotDocs: ', this.animalInfo) 中返回未定义。

Got Docs: 
 Observable {_isScalar: false, source: Observable, operator: MergeMapOperator}
 operator:MergeMapOperator {project: ƒ, resultSelector: undefined, concurrent: 
 Infinity}
 source:Observable {_isScalar: false, source: Observable, operator: MapOperator}
_isScalar:false
__proto__:
Object
Run Code Online (Sandbox Code Playgroud)

我不确定在 ngOnInit() 中使用上述代码是否是解决此问题的正确方法。

非常感谢任何帮助。

迈克尔

Jef*_*D23 5

阅读单个文档有一种更简单的方法。您已经有了 ID,因此您可以指向 ngOnInit 中的特定文档:

const docRef = this.afs.doc(`users/${this.curUser}/animals/${this.curPetID}`)
this.animalInfo = docRef.valueChanges()
Run Code Online (Sandbox Code Playgroud)

理想情况下,您将这些数据解包到 HTML 中并设置一个模板变量。

<div *ngIf="animalInfo | async as animal">

  Hello {{ animal.name }}

</div>
Run Code Online (Sandbox Code Playgroud)

或者你可以在组件 TypeScript 中订阅它。

animalInfo.subscribe(console.log)
Run Code Online (Sandbox Code Playgroud)