Reputation: 87
I want to return the data of a query in Angular Firestore:
public async getExercisePlan(patientUid: string) {
let plan: ExercisePlan = { name: '', userId: '', trainerId: '', trainings: []};
const query = this.firestore.collection(this.collectionName).ref.where('userId', '==', patientUid).limit(1);
await query.get().then(querySnapshot => {
if (querySnapshot.empty) {
console.log('no data found');
} else if (querySnapshot.size > 1) {
console.log('no unique data');
} else {
querySnapshot.forEach(documentSnapshot => {
return documentSnapshot.ref.get().then(res => { return res.data()});
});
}
});
The call of this method is:
this.exercisePlanService.getExercisePlan(this.patient.uid).then(res => console.log(res));
But the result is:
undefined
I get the data in the getExercisePlan() but cannot resolve it in the component!
Upvotes: 1
Views: 590
Reputation: 238
I have made a project with firestore and in my case, in order to retrieve documents, I had to write this code:
.then((querySnapshot) => {
tempDoc = querySnapshot.docs.map((doc) => {
return { id: doc.id, ...doc.data() };
});
Try to change your code to read docs inside querySnapshot and map them (in your case the only one retrieved). Another important tip is to return the id explicitly as first field.
Upvotes: 3