简体   繁体   English

离子2-无法访问角度2中其他功能分配的http提供程序分配的值

[英]Ionic 2 - Can not access value assigned by a http Provider by other functions in angular 2

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. 这是我的代码,我正在尝试通过setUserDetails函数在变量'user'中分配数据,该函数调用http提供程序函数fetchUserDetails,该函数按预期返回数据。 The getUserDetails prints null value. getUserDetails打印空值。

@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. 该事件发布了this.user,可以被应用程序组件访问,也可以正常工作。 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. 我希望在调用seUserDetails时在登录时设置一次用户数据,并且可以通过返回this.user来调用getUserDetails来在任何地方访问用户数据,但是它无法访问它。

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

Could you try this ? 你可以试试这个吗?

fetchUserDetails(){
let headers = new Headers();
headers.append('jwt', localStorage.getItem('token'));
return this.http.post(apiUrl+'get_user_details_api', {}, {headers: headers})
      .toPromise()
      .then(
          res => Promise.resolve(res.json()), 
          err => Promise.reject(err)
      );
}// end of get user details

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM