Here is my code I'm trying to assign data in variable 'user' by setUserDetails function which calls a http provider function fetchUserDetails which returns data as expected. The getUserDetails prints null value.
@Injectable()
export class UserDetailsProvider {
public userChanged = new EventEmitter();
private userUrl = apiUrl+'get_user_details_api'; // URL to web api
public user : any;
constructor(public http: Http, public authService: AuthServiceProvider, public events: Events) {
this.user = null;
}// end of constructor
fetchUserDetails(){
return new Promise((resolve, reject) => {
let headers = new Headers();
headers.append('jwt', localStorage.getItem('token'));
this.http.post(apiUrl+'get_user_details_api', {}, {headers: headers})
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}// end of get user details
setUserDetails(){
this.fetchUserDetails().then((result) => {
this.user = result;
console.log(result); // Added this as requested, I told you it's working
this.events.publish('UserLogged', this.user);
});
}// end of function
getUserDetails(){
//console.log(this.user); commenting this out to show the log of http then result
}// end of function
The event publishes the this.user and can be accessed by app component, that's also working fine. I want the user data to be set once at login when the seUserDetails is called and can be accessed the user data anywhere by just calling getUserDetails by returning this.user but it can not access it.
There is the output in console for you ...
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
VM17281 main.js:60356 Native: tried calling SplashScreen.hide, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
VM17281 main.js:33868 Object {user_id: "MEM17042246C9FE2C039", name: "aryan", is_active: "1", is_phone_verified: "1", email: "amankk92@hotmail.com"…}email: "amankk92@hotmail.com"ifile_name: "1705051493994229.jpg"ipath: "uploads/MEM17042246C9FE2C039 /profile_pic"is_active: "1"is_phone_verified: "1"login_count: "0"name: "aryan"user_id: "MEM17042246C9FE2C039"user_type: "3"__proto__: Object