Reputation: 273
How to make it work okay? I need only one document from base with id.Is it possible to subscribe it? Because it returns me a Observable object:(
Here`s my code.
getByid(id){
return this.itemscollection.doc('9K6ue-afwwafwaf').valueChanges();
}
Upvotes: 5
Views: 13440
Reputation: 4441
If you also want the id
of the document and use an observable instead, do:
this.firestore.collection('collection').doc('someid').snapshotChanges().subscribe(
res => {
this.item= { id: res.payload.id, ...res.payload.data() as InterfaceName };
},
err => {
console.debug(err);
}
)
Upvotes: 1
Reputation: 595
First thing first, you should make sure that your id is right.
And I assume you used AngularFirestore
to connect to Firebase.
public this.itemsCollection = null;
public constructor(private af: AngularFirestore) {
this.itemCollection = this.af.collection('Your-colection-name');
}
public getById(docId: string): any {
return this.itemCollection
.doc(docId)
.valueChanges()
.subscribe(item => {
return item;
// If you prefer including itemId back to object
// return {...item, id: docId}
});
}
Upvotes: -1
Reputation: 840
Let's try to convert the observable to promise like this
async getDocument(docId:string){
let document = await this.afs.doc(docId).get().toPromise();
return document.data();
}
Upvotes: 3
Reputation: 189
I think you are trying to do this:
constructor(db: AngularFirestore){
db.collection('yourcollection').doc("documentid").ref.get().then(function (doc) {
if (doc.exists) {
console.log(doc.data());
} else {
console.log("There is no document!");
}
}).catch(function (error) {
console.log("There was an error getting your document:", error);
});}
The above is the simplest way I know to read a document on firestore. It will show the internal data from your document. I hope it helps to you.
Upvotes: 10