One of the methods in my service aims to return an Observable of the User object. Below is the code snippet I have written:
constructor(private firestore: AngularFirestore,
private db: AngularFireDatabase) { }
/**
* Retrieve user based on provided email address, return null if not found
*
* @param email The email to search for
*/
getUserByEmail(email: string): Observable<User> {
var user: User;
let userRef = this.firestore.collection("users").ref.where('email', '==', email);
userRef.get().then(res => res.forEach(userDoc => {
user = userDoc[0].data() as User;
user.id = userDoc[0].id;
console.log(user);
}));
console.log(user);
return of(user);
}
In my component, I require this data for subsequent actions. Therefore, I implemented the following:
checkEmail() {
var user$: User
this.us.getUserByEmail(this.existingUserForm.value.email).subscribe(user => {
console.log(user);
if (user) {
this.msg$ = "success";
// proceed with next steps
}
else {
this.msg$ = "User with this email does not exist!";
}
});
}
I am uncertain about how to properly return an observable from the service in order to utilize the data within my component. Additionally, I question the correctness of the approach I am taking. Any suggestions would be greatly appreciated.