Ionic 2 Promise returns __zone_symbol__value array

0

I am using Ionic 2 and I have a provider that is responsible for making http requests to a server.

I use the following block of code to make the request.

Api_get(data?){
     return new Promise((resolve, reject) =>{
         this.http.post(this.url + data.method, data.send).subscribe(data =>{
             resolve( data.json() );
             reject({status:false});
         })
     })

  }

The request is executed and it brings the data but in an object __zone_symbol__value array [] I do not know if I'm doing something wrong I hope you can help me.

    
asked by Jorge Mejia 28.04.2017 в 01:01
source

1 answer

0

You need to map your answer in a provider for example a case used for a login look:

PROVIDER:

import {Http, Headers} from '@angular/http';
@Injectable()
export class LoginService {
  public headers;

  constructor(private _http: Http) {
  }

  login(username, password) {
    this.headers = new Headers();
    this.headers.append('Accept', 'application/json');
    this.headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let body = 'username=${username}&password=${password}';
    return this._http.post('http://localhost/api/login', body, { "headers": this.headers })
      .map(res => res.json())
      .catch((err:any)=> {
        return Observable.of(undefined)
      });
  }
}

PAGINA.ts:

import { LoginService } from '../../providers/login-service'

@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
  providers: [ LoginService, Globals, UsersService, Keyboard]
})

@Injectable()
export class LoginPage {
  constructor(private _loginService: LoginService) {}
login(){
var username = this.user.username;
var password = this.user.password;
this._loginService.login(username, password)
  .subscribe(data => {
    if(data === undefined){
      console.log("USUARIO INVALIDO");
    } else {
      //Datos del usuario
      console.log(data); 
    }
  });
}
}

But if you need to map it with the .map of the Promise function, check this code as an example and I hope it serves you, greetings.

    
answered by 28.04.2017 в 22:14