E. Tallas
E. Tallas

Reputation: 23

Ionic and AngularFire2: How to retrieve data from a FireStore document?

I am probably missing some basics, hopefully this question is not too dumb...

I am trying to retrieve data from a document stored in FireStore. I am following the example provided here: https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md

What I am wondering: After having access to the document:

this.itemDoc = afs.doc<Item>('items/1');
this.item = this.itemDoc.valueChanges();

How can I retrieve data from that document? I don't want . to use it in my html, but get the data from some fields and do something with it in TypeScript.

Obviously this.item is not an array so item[0].fieldname doesn't work...

TIA.

Upvotes: 1

Views: 2142

Answers (2)

Pratik Gadge
Pratik Gadge

Reputation: 11

//I assume you know how to use firebase with angular and ionic 
import { AngularFirestore } from 'angularfire2/firestore'; 

 constructor(private firestore:AngularFirestore)
     {
}


readNote(){
    this.readUsingObservable().subscribe(data => {
      console.log(data[0].textnote); //0 means accessing first object
     console.log(data[0].day);
//textnote and day are fields in collection object

    })
}

 readUsingObservable():Observable<any>{
    return this.firestore.collection('yourcollectionname').valueChanges();
  }

Upvotes: 0

Hareesh
Hareesh

Reputation: 6900

this.item is an observable, you need to subscribe to get the data.

this.itemDoc = afs.doc<Item>('items/1');
this.item = this.itemDoc.valueChanges();
this.item.subscribe(res=>{
    console.log(res.fieldname);
}

Upvotes: 1

Related Questions