Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Retrieve user details from service in Angular

Angular 12; .NET Core 3.1 Following a code-maze tutorial, I have a working authentication (registration/login) service using .NET Core Identity. After login, I would like to add 'username' data ...

1 answer  ·  posted 2y ago by mcalex‭  ·  last activity 2y ago by meriton‭

#1: Initial revision by user avatar mcalex‭ · 2021-10-06T08:50:46Z (over 2 years ago)
Retrieve user details from service in Angular
Angular 12; .NET Core 3.1

Following a [code-maze tutorial](https://code-maze.com/angular-authentication-aspnet-identity/), I have a working authentication (registration/login) service using .NET Core Identity.  After login, I would like to add  'username' data to my app's header component and a 'Welcome username' message in the home component.

I used `ng g s shared/user` to create a user service.  The service's getCurrentUser() code:

```ts
public currentUser?: any;
...
getCurrentUser(): Observable<any> {
  if (this._authService.isAuthenticated) {
    this._repoService.getData('api/accounts/user')
        .subscribe(res => {
          this.currentUser = res;
          console.log('UserService: currentUser = ' + this.currentUser?.email);
    });
  }
  return this.currentUser;
}
```

My header component and home component attempt to display the user's name (email) with the following:
```ts
getUsername(): string {
  this._userService.getCurrentUser().subscribe(
      res => { 
        this.userName = (res as ApplicationUserDTO).email;
      }
  );
  return this.userName;
}
```

This does not result in the name being displayed.  The browser console shows:  
```ERROR TypeError: Cannot read properties of undefined (reading 'subscribe')```. 

 Navigating to another page and back to the home page results in a slightly different console error:  ```ERROR TypeError: this._userService.getCurrentUser(...).subscribe is not a function```

However the console.log call in the user.service.ts correctly shows my logged in user's email.

The service originally tried returning an `ApplicationUserDTO` which was resulting in only the `...subscribe is not a function` error.  I checked this on SO and found [Angular subscribe to a service: subscribe is not a function](https://stackoverflow.com/questions/58892754/angular-subscribe-to-a-service-subscribe-is-not-a-function) and adjusted my service code to return `Observable<any>`.  Making this change, added the `... cannot read properties of undefined (reading 'subscribe')` error.

Where have I gone wrong?