我想使用 AngularFire 阅读 Firebase Firestore 中的特定文档。
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AngularFirestore , AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs';
import { async } from 'q';
@Component({
selector: 'app-read-more-page',
templateUrl: './read-more-page.component.html',
styleUrls: ['./read-more-page.component.scss']
})
export class ReadMorePageComponent implements OnInit {
postCatagory: string;
databaseName: any;
uuid: string;
documentObject:any; //real docum
//observables
items: Observable<any[]>;
doc : AngularFirestoreDocument<any>
post : Observable<any>;
constructor(private route: ActivatedRoute , private afs: AngularFirestore ) {
this.databaseName = this.route.snapshot.paramMap.get('category');
this.items = afs.collection(this.databaseName).valueChanges();
}
ngOnInit() {
if(this.route.snapshot.paramMap.get('uuid') != null){
//when id is not null
this.databaseName = this.route.snapshot.paramMap.get('category');
this.uuid = this.route.snapshot.paramMap.get('uuid');
console.log("UUID " , this.uuid);
console.log("category " ,this.databaseName);
//read data from collection
//read post
this.doc = this.afs.doc(`${this.databaseName}/${this.uuid}`);
}
}
}
${this.databaseName}/${this.uuid}
是我想阅读的文件。但作为 firestore 文档中的示例,我们可以轻松地读取集合中的所有文档。但我只需要检索确切的文档。我还是个初学者,如果你能提供一些适合初学者的内容,我将不胜感激
最佳答案
const docRef = afs.collection('collectionName').doc('documentId');
它会给你文档引用,然后你可以订阅
snapshotChanges()
或调用data()
或其他方法
关于javascript - firebase firestore 以 Angular 读取特定文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58293130/