Nip*_*a C 5 javascript firebase angularfire2 angular google-cloud-firestore
我想使用 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}`);
}
}
}
Run Code Online (Sandbox Code Playgroud)
${this.databaseName}/${this.uuid}是我想阅读的文件。但是作为 firestore 文档中的示例,我们可以轻松读取集合中的所有文档。但我只需要检索确切的文档。
我还是个初学者,所以如果你能提供一些适合初学者的内容,我将不胜感激
const docRef = afs.collection('collectionName').doc('documentId');
Run Code Online (Sandbox Code Playgroud)
它会给你文档参考,然后你可以订阅snapshotChanges()或调用data()或其他方法