Ionic get RESTful API issue


#1

I am getting a RESTful API, and I have some question about the getting JSON, here is the JSON I get:

{
“result”: “success”,
“user”: {
“loggedIn”: true,
“name”: “Nulra”,
“password”: “”
}
}
And the part for getting the JSON in auth-service.ts:

login(credentials) {

if(this.data){
    return Promise.resolve(this.data);
}

let opt: RequestOptions;
let myHeaders: Headers = new Headers;

myHeaders.set('Accept', 'application/json; charset=utf-8');
myHeaders.append('Content-type', 'application/json; charset=utf-8');
opt = new RequestOptions({
    headers: myHeaders
})

return new Promise(resolve => {
    this.http.get(apiUrl+'login/0/login?email='+credentials.email+'&password='+credentials.password, opt)
    .map(res => res.json())
    .subscribe(data => {
        this.data = data;
        resolve(this.data);
    });
});

And in my login.ts:

doLogin() {
   this.authService.login(this.loginData).then(data => {
      console.log("NULRA CHECKING: ",data);
   }, (err) => {

   });
}

I discover it just getting the user part in JSON, and missing the result part in JSON when I inspect in the console log. How can I get the both result and user part?

the console log:

Object {loggedIn: true, name: “Nulra”, password: “”}

Anybody have idea, hope for a help, Thanks a lot!!


#2

You are perpetuating the explicit promise construction antipattern and not propagating errors correctly. If you insist on caching results in this service, I would do so via an exposed promise instead.

I don’t see any code indicating where you are inspecting anything via a console log, but if it’s not showing what you expect, chances are that you’re doing it in a scope where you cannot safely rely on things having resolved.


Ionic - unable to retrieve/display data using navcontroller
#3

HI rapropos, thanks for your reply. I am beginner of ionic, I am little confused about what you saying, can you explain me more detail, sorry for that.


#4

I find it may be cache problem, please i get other json with same domain, it give me a same object, anyone have idea?