gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:00:59 25 4
gpt4 key购买 nike

我正在尝试从 firestore 子集合中检索单个文档:数据库/用户/uid/动物/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);
}
});
}
}

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

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

当我运行代码时,在 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

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

非常感谢任何帮助。

迈克尔

最佳答案

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

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

理想情况下,您在 HTML 中解包此数据并设置模板变量。

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

Hello {{ animal.name }}

</div>

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

animalInfo.subscribe(console.log)

关于Angular Firestore 如何检索子集合文档进行编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49269922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com