Is there a way to get real-time data from a Firestore document using a service?
According to Firebase's documentation, you can achieve this by following this link: https://firebase.google.com/docs/firestore/query-data/listen?hl=es#web-modular-api
I have successfully retrieved the data directly from the component, but I'm facing issues when trying to do it from a service.
I am currently working with Angular 17 and Angularfire 17.
Here is what I have tried:
service.ts
import { Firestore, doc, onSnapshot } from '@angular/fire/firestore';
private firestore = inject(Firestore);
async getUserMy(uid: string) {
const docRef = doc(this.firestore, 'users', uid);
const userMy = onSnapshot(docRef, async (doc) => {
const user = doc.data() as UserMy;
return user;
});
return userMy;
}
components.ts
private fs = inject(FirebaseService);
ngOnInit() {
this.fs.getUserMy('xxxxxxxxx').then( (result) => {
console.log(result);
});
}
However, the output in the console is not as expected:
ƒ () {
setTimeout(() => {
if (macrotask && macrotask.state === "scheduled") {
macrotask.invoke();
}
}, 10);
return ret.apply(this, arguments);
…