Scenario
In the development process, I am using an auth.service.ts
. This service is responsible for fetching user information from the database
upon login. The retrieved data is then used to create a new user object
. Here is a snippet of the code:
user: User;
this.user = res;
console.log(this.user);
The output of this code block looks like this:
Object account_locked: false affiliate_owner: null business_phone: "8178966767" city_address: null contract: false created_on: null dob: "1984-03-05T06:00:00.000Z" email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9ccd1c8c4d9c5cce9ccc4c8c0c587cac6c4">[email protected]</a>" employee_owner: null fax_number: "1234567" first_name: "Nicholas" home_phone: "1234567" id: 7last_name: "theman" middle_name: "dude" mobile_phone: "1234567" ssn: "123456789" state_address: null street_address: null user_auth_level: 1 zip_address: null __proto__: Object
The issue arises when trying to access the user object
in another class by importing the auth.service.ts
.
import { Auth } from './../services/auth.service';
constructor( private router: Router, private auth: Auth, private http: Http ) { }
However, the following line of code returns undefined:
ngOnInit() {
console.log(this.auth.user);
}
Similarly, attempting to utilize interpolation in the template {{auth.user.email}} does not yield the desired result.
Query
How can I efficiently access the user object
generated in auth.service.ts
from a different class, specifically profile.component.ts
and its corresponding HTML template profile.component.html
?
Could this issue potentially be related to inheritance
?
Essential Code Snippets
auth.service.ts
export class Auth {
lock = new Auth0Lock(myConfig.clientID, myConfig.domain, options, {});
userProfile: Object;
logreg: LogReg;
user: User;
constructor(private router: Router, private http: Http ) {
// create users
this.logreg = new LogReg(profile.email);
this.checkRegister(this.logreg).subscribe((res)=>{
//do something with the response here
this.user = res;
console.log(this.user);
});
}
}
I aim to retrieve the user object
defined above in the following context:
profile.component.ts
import { Auth } from './../services/auth.service';
@Component({
providers: [ Auth ],
templateUrl: './profile.component.html'
})
export class ProfileComponent implements OnInit {
constructor( private router: Router, private auth: Auth, private http: Http ) { }
ngOnInit() {
console.log(this.auth.user);
}
}