the problem is that the console represent ‘undefined’.
this.firestore.collection('list').doc('1').get().subscribe((doc: any) => {
this.myAns = doc.data().answer ? doc.data().answer : "no answer";
});
console.log('this.myAns', this.myAns);
but, I want to get right answer in console.
this.firestore.collection('list').doc('1').get().subscribe(async (doc: any) => {
this.myAns = await doc.data().answer ? doc.data().answer : "no answer";
});
console.log('this.myAns', this.myAns);
i tried this one. but not working.
this.firestore.collection('list').doc('1').get().subscribe((doc: any) => {
this.myAns = doc.data().answer ? doc.data().answer : "no answer";
console.log('this.myAns', this.myAns);
});
i know this works well, but what I want is represent outside of this.firestore.
please, help me..
2
Answers
You should this.myAns inside the subscribe block. If you need to use this.myAns outside of the subscribe block, you can trigger the necessary logic from within the subscribe block or use a callback function.
The Firebase API supports this out-of-the-box (see documentation, "Get a document"):