I'm currently trying to update a global variable foundProfile
inside a map() function. Now, I'm half realising that this may not be the way I should be doing things - but I can't figure out the reason nor can I think of any other way to set the variable to true if there's a match. Here's my current code:
export class LoginComponent implements OnInit {
private usernameTxt: string;
private passwordTxt: string;
private foundProfile: boolean;
constructor(private router: Router, private page: Page, private loginService: LoginService, private profileService: ProfileService) {
page.actionBarHidden = true;
}
ngOnInit() {
this.usernameTxt = 'someUsername';
this.passwordTxt = 'SomePassword321!';
this.foundProfile = false;
}
getProfile(username: string, password: string) {
return this.loginService.getProfiles()
.map(
profiles => profiles.filter(
(profile => {
if(profile.email == username && profile.password == password) {
this.foundProfile = true; //Set to true so we know a profile has been found
return profile;
}
})
)
);
}
doLogin() {
if(this.checkValues(this.usernameTxt, this.passwordTxt)) {
this.profileService.profile = this.getProfile(this.usernameTxt, this.passwordTxt);
console.log(this.foundProfile);
}
}
}
Now, my questions is as follows:
Why is this.foundProfile = true;
inside the map()
function not setting private foundProfile: boolean;
to true
? I'm sure I'm going about this the wrong way - but why is that the case and what should I be doing instead?
Thanks in advance.
edit: in my loginService, I'm initiating my observable like so:
getProfiles(): Observable<Profile[]> {
return this.http.get<Profile[]>(this.url);
}