Refactor your code into readable format.

within your services return the observable, don't subscribe within the
service as it is a bad practice.

login(email: string, password: string): Observable<any> {
return this.checkLogin(email, password).pipe(
tap((user) => console.log(user)) // Optional logging
);
}

And in your component class you can use like this

this.authService.login(this.email, this.password).subscribe((user) => {
this.user = user;
});




On Tue, Feb 18, 2025 at 1:33 PM Siddharth Shankar <siddharth.1...@gmail.com>
wrote:

> Hi,
> I am getting user object as undefined though the method that is returning
> the user object is not null. Please help
>
> Component call :
> this.user = this._authService.login(this.email, this.password)
> // here this.user is comming undefined
>
> Service method
> login(email : string , password : string): User {
>
>   this.checkLogin(email, password).subscribe((data)=>{
>   //console.log(data);
>   this.usr = data;
>   console.log(this.usr.name);
>   if(this.usr != null)
>   {
>    return this.usr;
>    // this.usr has value
>   }
>   })
>   }
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Angular and AngularJS discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+unsubscr...@googlegroups.com.
> To view this discussion visit
> https://groups.google.com/d/msgid/angular/d7065033-73df-4952-a2c7-d159b2939457n%40googlegroups.com
> <https://groups.google.com/d/msgid/angular/d7065033-73df-4952-a2c7-d159b2939457n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 

*Mohdmmad Imran*

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/angular/CAJrQtiYWbFNR%3DXN%3Dt19L9b4Er5s1gTBP0VHYobzm948xiWW6HA%40mail.gmail.com.

Reply via email to