Reputation: 130
I think I'm making some silly mistake when trying to create a function to get a single document's data from firestore in Angular.
public async getPage(id){
const ref = this.db.collection('pages').doc(id).get().data();
}
This was my original code. I hadn't gone further than the fetch because I got the error:
Property 'data' does not exist on type 'Observable<DocumentSnapshot<DocumentData>>'
I also tried the following, with the same error:
public async getPage(id){
const ref = await this.db.collection('pages').doc(id).get().data();
}
public async getPage(id){
const result = await this.db.collection('pages').doc(id).get()
console.log(result.data())
}
Upvotes: 0
Views: 58
Reputation: 1053
You must either subscribe to the observable get()
or convert into a promise using toPromise()
to await the call.
export class PageServices {
public getPage(id: string) {
this.db.collection('pages').doc(id).get().subscribe((res => {
console.log(res.data());
}));
}
// or using `toPromise`
public async getPageAsync(id: string) {
const docSnapshot = await this.db.collection('pages').doc(id).get().toPromise();
if (docSnapshot.exists) {
console.log(docSnapshot.data());
}
}
}
Upvotes: 1