javascript - firebase firestore 以 Angular 读取特定文档

标签 javascript angular firebase google-cloud-firestore angularfire2

我想使用 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/

相关文章:

java - 使用 Firebase-UI (Android) 的 Firestore 聊天应用

javascript - Firebase onAuthStateChanged 和 Navigation Guards 的模式 - Quasar 应用

javascript - IE 11 浏览器上的 Bing map ,鼠标单击事件仅在我们打开兼容模式时有效

javascript - 将我的弹出窗口居中的问题

javascript - 无法访问现有 Node 包的属性,因为它是 'undefined' 。为什么?

javascript - 以 Angular 添加 AsyncValidator 时出错

json - Angular 4 + Express 未在 Heroku 上启动

forms - 使用@ViewChild 获取输入的 .nativeElement 返回 'undefined'

firebase - 如何在 Flutter 中查询其字段值包含在本地列表变量中的 Firestore 数据

javascript - 我不知道为什么我会得到这个,如果它是根据 React 手册的话