I am making a simple angular application trying to learn angular. I am using an online api from json placeholder for backend which stores my login credentials. I am retrieving these using an http.get request. When I login for the first time the http request returns undefined but the second time it returns the correct credentials. I am confused at this behavior
My login function:
loginUser(email: string, password: string)
{
this.data.getCreds().subscribe(data => {
this.mail = data.credentials[0].email;
this.pass = data.credentials[0].password;
});
console.log(this.mail,email,this.pass,password, 'here!!!');
if(this.mail==email&&this.pass==password)
{
this.isLoggedIn = true;
localStorage.setItem('loggedIn', 'true');
this.router.navigate(['/admin']);
}
}
My getCreds function from the data service:
getCreds(): Observable<any>{ return this.http.get("https://my-json-server.typicode.com/divyanshu-avi/login/db").pipe(map(data => data));
}