I'm trying to get a simple value from a record in the Cloud Firestore using AngularFire.
I've had a difficult time getting my head around RxJS and Observables, and I have mostly got there (sort of), but sometimes I don't want an Observable stream of data, I just need a single, static value, from a single record found using a non-id query.
I have gone through the documentation but can't seem to find any examples.
This code is my crude and inelegant attempt, but there must be a better pattern? Can anyone suggest a better pattern?
Please Note This code is to illustrate the non-pattern, it hasn't been coded into a project or executed so there may be some typos/errors!
import { AngularFirestore } from '@angular/fire/firestore';
import { Membership } from 'src/app/models/membership.model';
@Injectable({
providedIn: 'root'
})
export class MembershipService {
email: string;
constructor(private afs: AngularFirestore) {}
findEmailByPhone(phoneNumber: number) {
this.afs.collection<Membership>('memberships', query => query.where('phonenumber', '==', phoneNumber).limit(1)).valueChanges().pipe(first()).subscribe(results => {
if (results.length == 1) {
return this.email = results[0].email;
}
});
}
}
Aucun commentaire:
Enregistrer un commentaire