firebase firestore 读取 angular 中的特定文档
firebase firestore read a specific document in angular
我想使用 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 docs 中的示例,我们可以轻松地读取集合中的所有文档。但我只需要检索确切的文档。
我还是初学者,如果您能提供一些适合初学者的内容,我将不胜感激
const docRef = afs.collection('collectionName').doc('documentId');
会给你文档参考,然后你可以订阅snapshotChanges()
或调用data()
或其他方法
像这样轻松俗气地访问文档数据:
let data
try {
const userDoc = this.afs.doc(`users/${uid}`)
const snap = await userDoc.get().toPromise()
data = snap.data()
} catch (err) {
console.error(err)
data = { videoGroups: [], roles: [] }
}
显然,上面的内容存在于 async
方法中。
如果您没有使用 async await
那么这就是您想要的:
let data
const userDoc = this.afs.doc(`users/${uid}`)
userDoc.get().toPromise().then((snap) => {
data = snap.data()
}).catch((err) => {
console.error(err)
data = { videoGroups: [], roles: [] }
})
请注意,上述方法获取的是您请求时的文档。这称为快照。将此比作拍摄/拍摄在单个时间点拍摄静止图像的照片。
如果您想从单个文档流式传输数据,那么您应该使用 valueChanges()
,如 here
请注意,您需要subscribe()
到valueChanges()
我想使用 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 docs 中的示例,我们可以轻松地读取集合中的所有文档。但我只需要检索确切的文档。
我还是初学者,如果您能提供一些适合初学者的内容,我将不胜感激
const docRef = afs.collection('collectionName').doc('documentId');
会给你文档参考,然后你可以订阅snapshotChanges()
或调用data()
或其他方法
像这样轻松俗气地访问文档数据:
let data
try {
const userDoc = this.afs.doc(`users/${uid}`)
const snap = await userDoc.get().toPromise()
data = snap.data()
} catch (err) {
console.error(err)
data = { videoGroups: [], roles: [] }
}
显然,上面的内容存在于 async
方法中。
如果您没有使用 async await
那么这就是您想要的:
let data
const userDoc = this.afs.doc(`users/${uid}`)
userDoc.get().toPromise().then((snap) => {
data = snap.data()
}).catch((err) => {
console.error(err)
data = { videoGroups: [], roles: [] }
})
请注意,上述方法获取的是您请求时的文档。这称为快照。将此比作拍摄/拍摄在单个时间点拍摄静止图像的照片。
如果您想从单个文档流式传输数据,那么您应该使用 valueChanges()
,如 here
请注意,您需要subscribe()
到valueChanges()