I'm using angularfire2 v6 and angular 11. I'm simply trying to get a single document from the users collection based on their email. I don't want to use valueChanges()
or snapshotChanges()
as the user object won't be updated, and if it is, I don't care to get realtime updates. I just want to get the data once when the user navigates to their profile. I've seen that you should be able to use get()
instead of the aforementioned methods, but I can't get anything to work correctly.
Doing this works fine:
let query = (this.firestore.collection('users', ref => ref.where('email', '==', res.email)).valueChanges());
// subscribe to changes
query.subscribe(queriedItems => {
this.item = queriedItems[0];
this.firstName = this.item.firstname;
this.lastName = this.item.lastname;
this.email = this.item.email;
this.role = this.item.role;
this.lead = !!this.item.lead;
});
But again, doesn't valueChanges()
by nature keep listening for updates? I don't want to do that. And replacing .valueChanges()
with .get()
means I can't subscribe, and using toPromise().then()
seems to just return junk and not the actual document.
Couldn't figure out how to make the get()
work at all, but I did find a workaround by setting it to a subscription, then unsubscribing once you get the data.
this.fireAuth.currentUser.then((res) => {
let query = (this.firestore.collection('users', ref => ref.where('email', '==', res.email)).valueChanges());
// subscribe to changes
this.profileSubscription = query.subscribe(queriedItems => {
this.item = queriedItems[0];
this.firstName = this.item.firstname;
this.lastName = this.item.lastname;
this.email = this.item.email;
this.role = this.item.role;
this.lead = !!this.item.lead;
//Unsubscribe here if you don't want an active listener to show changes
this.profileSubscription.unsubscribe();
});
});