<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dmccolloughOneGas</title>
    <description>The latest articles on DEV Community by dmccolloughOneGas (@dmccolloughonegas).</description>
    <link>https://dev.to/dmccolloughonegas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F576678%2F8f6e4628-9f60-4909-ab9d-53b667df191d.png</url>
      <title>DEV Community: dmccolloughOneGas</title>
      <link>https://dev.to/dmccolloughonegas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dmccolloughonegas"/>
    <language>en</language>
    <item>
      <title>Trouble with Angular ngOnInit</title>
      <dc:creator>dmccolloughOneGas</dc:creator>
      <pubDate>Tue, 09 Feb 2021 19:51:07 +0000</pubDate>
      <link>https://dev.to/dmccolloughonegas/trouble-with-angular-ngoninit-10g3</link>
      <guid>https://dev.to/dmccolloughonegas/trouble-with-angular-ngoninit-10g3</guid>
      <description>&lt;p&gt;I have an app that I inherited from somebody that left our organization.&lt;br&gt;
I’m having a problem with ngOnInit not working the way I think it should.&lt;br&gt;
I’m still pretty new using Angular and Observables&lt;/p&gt;

&lt;p&gt;I can see in the console when I navigate to the component it entering the ngOnInit method, what I don’t see is the console.info statement in the subscribe for the response being executed.&lt;br&gt;
Once I’m in the component, I can refresh the page and then I can see the console.info statement in the subscribe.&lt;/p&gt;

&lt;p&gt;My question is why don’t I see the console.info statement when I first navigate to the component?&lt;/p&gt;

&lt;p&gt;Component ngOnInit method&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ngOnInit(): void {

  console.info('Entering ngOnInit - Home Component');

  this.profile.getProfile().subscribe((resp) =&amp;gt; {

      this.currentUser = this.local.getObject(StorageItems.UserProfile) as IMSALUserProfile;

      console.info('Current User: ', + JSON.stringify(this.currentUserInit));
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is what my service looks like, it’s a service that is fetching user profile information from Azure Active Directory using MSAL&lt;br&gt;
import { HttpClient, HttpHeaders } from '@angular/common/http';&lt;br&gt;
import { Injectable } from '@angular/core';&lt;br&gt;
import { BaseService } from './base.service';&lt;br&gt;
import { AuthError } from '@azure/msal-browser';&lt;br&gt;
import { LoggerService } from './logger.service';&lt;br&gt;
import { Observable, of } from 'rxjs';&lt;br&gt;
import { IMSALUserProfile } from '../../shared/interfaces/msaluserprofile';&lt;br&gt;
import { SessionService } from './session.service';&lt;br&gt;
import { StorageItems } from '../../shared/interfaces/enums/storage.model';&lt;br&gt;
import { LocalStorageService } from './local-storage.service';&lt;br&gt;
import { UserService } from './user.service';&lt;br&gt;
import { IUserInit } from '../../shared/interfaces/userinit';&lt;br&gt;
@Injectable({&lt;br&gt;
  providedIn: 'root'&lt;br&gt;
})&lt;br&gt;
export class UserProfileService extends BaseService {&lt;/p&gt;

&lt;p&gt;currentUser!: IMSALUserProfile;&lt;br&gt;
  currentUserInit!: IUserInit;&lt;/p&gt;

&lt;p&gt;constructor(private http: HttpClient, &lt;br&gt;
      private logger: LoggerService, &lt;br&gt;
      private session: SessionService,&lt;br&gt;
      private local: LocalStorageService, &lt;br&gt;
      private userInit: UserService) {&lt;br&gt;
    super();&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;public getProfile(): Observable {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sessionUser = this.session.getItem(StorageItems.UserProfile);

if (sessionUser.length !== 0) {
  this.currentUser = JSON.parse(this.session.getItem(StorageItems.UserProfile));
}

let profile!: IMSALUserProfile;

if (this.currentUser) {

  profile = this.currentUser as IMSALUserProfile;
} else {
  this.http.get('https://graph.microsoft.com/v1.0/me')
  .subscribe({
    next: (profile) =&amp;gt; {
      profile = profile;

      this.local.setItem(StorageItems.UserProfile, profile);
      this.session.setItem(StorageItems.UserProfile, JSON.stringify(profile));

      this.currentUser = profile as IMSALUserProfile;
    },
    error: (err: AuthError) =&amp;gt; {
      console.info('Authentication error');
    }
  })
}

this.local.setItem(StorageItems.UserProfile, profile);
this.session.setItem(StorageItems.UserProfile, JSON.stringify(profile));

return of(profile);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
}&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
