Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Promete y suscríbete

Tengo una aplicación Angular2 (ionic2). Tengo una función que solicita ciudades, pero aparece el error de que Property subscribe no existe en this.cityService.getAllCities() .

cityPage.ts tiene una función como esta:

 getCities(){ this.cityService.getAllCities() .subscribe(cityData => { this.cityList = cityData; }, err => console.log(err), () => console.log('Complete!') ); }

mi función cityService.getAllCities() se ve así:

 getAllCities(){ return new Promise (resolve => { this.storage.ready().then(() => { this.storage.get('authData').then(authData => { let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' + authData.access_token }); let opt = new RequestOptions({ headers: hdr }); return this.http.get(AppSettings.API_GET_CITIES).map(res => <CityModel[]> res.json(), opt); }).catch(() => { //resolve(false); }); }); }); }

Editar

Según el comentario, he cambiado mi función de esta manera:

 getAllCities(){ return Observable.create(resolve => { this.storage.ready().then(() => { this.storage.get('authData').then(authData => { let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' + authData.access_token }); console.log('access_token ' + authData.access_token); let opt = new RequestOptions({ headers: hdr }); return this.http.get(AppSettings.API_GET_CITIES,opt).map(res => <CityModel[]> res.json()).subscribe((result) => { console.log(result); resolve = result; }); }).catch(() => { //resolve(false); }); }); }); }

En mi console.log(result) recibo datos, pero los datos nunca se devuelven a mi función getCities() . Además, no se llama a console.log('Complete!') .

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La razón por la que arroja un error, porque el método .subscribe está disponible en Observable para escuchar, cada vez que emite datos. Aquí, desde el método getAllCities , está devolviendo una promise que podría aplicar .then , funcione sobre ella para obtener los datos devueltos de esa Promise .

 getCities() { this.cityService.getAllCities() .then( cityData => { this.cityList = cityData; }, err => console.log(err), () => console.log('Complete!') ); }

Y también devolver la promesa del método getAllCities llamando al método .toPromise() sobre http.get() Observable.

 getAllCities(){ return new Promise (resolve => { this.storage.ready().then(() => { this.storage.get('authData').then(authData => { let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' + authData.access_token }); let opt = new RequestOptions({ headers: hdr }); //returned promise from here. return this.http.get(AppSettings.API_GET_CITIES) .map(res => <CityModel[]> res.json(), opt) .toPromise(); }).catch(() => { //resolve(false); }); }); }); }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!