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

178
Views
AQUÍ: Devuelva el Observable mientras espera el cambio de estado para finalizar y luego suscríbase.

Estoy encontrando un momento difícil para resolver estos 2 problemas y sigo trabajando en ello. Espero que alguien pueda ayudarme. ¡Gracias por adelantado!

1.) Tengo un map.service con esta función.

 readKML(file): any { const exportUrl = URL.createObjectURL(file); const reader = new HERE_MAP.data.kml.Reader(exportUrl); // Parse the document reader.parse(); reader.addEventListener('statechange', (evt) => { if (evt.state === HERE_MAP.data.AbstractReader.State.READY) { return of({ markers: ['test'], lines: ['test] }); } if (evt.state === HERE_MAP.data.AbstractReader.State.ERROR) { this.message.error('KML parsing error'); return of(null); } }); }

Esta función se llama en el componente y espera obtener los datos de retorno.

 this.map.service.readKML(file) .subscribe( data => { console.log(data); } )

el problema es que me sale un error

 this.map.readKML(...).subscribe is not a function

Expectativa:

Los datos deberían recibirse si stateChange es igual a 'READY'.

2.) El servicio se llamará al mismo tiempo con un valor de parámetro diferente y se espera que tenga diferentes retornos si el número 1 está bien.

Sin embargo, esto se puede resolver si 1 sigue siendo un problema.

 this.map.service.readKML(file1) .subscribe( data => { console.log('one', data); } ) this.map.service.readKML(file1) .subscribe( data => { console.log('two', data); } )

Si tienen una idea sobre cómo solucionar este u otro enfoque, se lo agradeceremos mucho. ¡Gracias!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Problema: En primer lugar, por qué falló su código: addEventListener es una operación asíncrona y devuelve el valor void debido of que ni siquiera se alcanzó cuando su función ya regresó de readKML .

 readKML(file): any { const exportUrl = URL.createObjectURL(file); const reader = new HERE_MAP.data.kml.Reader(exportUrl); // Parse the document reader.parse(); // 'addEventListener' is an async operation which returns void, so you are returning a void reader.addEventListener('statechange', (evt) => { if (evt.state === HERE_MAP.data.AbstractReader.State.READY) { return of({ // reached here after already returning void markers: ['test'], lines: ['test'] }); } if (evt.state === HERE_MAP.data.AbstractReader.State.ERROR) { this.message.error('KML parsing error'); return of(null); } }); }

Arreglo: necesitas devolver un observable. Así que envuelva addEventListener en un observable. Completa el observable una vez que llegue la respuesta.

Dado que está devolviendo observable, cada vez que llame a este método readKML se creará un nuevo observable.

 readKML(file): any { const exportUrl = URL.createObjectURL(file); const reader = new HERE_MAP.data.kml.Reader(exportUrl); reader.parse(); return new Observable((subscriber) => { // return observable reader.addEventListener('statechange', (evt) => { if (evt.state === HERE_MAP.data.AbstractReader.State.READY) { subscriber.next({ markers: ['test'], lines: ['test'] }); // return response subscriber.complete(); // always complete for next so only single reponse goes for single method call (cold observable) } if (evt.state === HERE_MAP.data.AbstractReader.State.ERROR) { this.message.error('KML parsing error'); subscriber.next(null); // you can also throw error ` throw throwError(response); `; subscriber.complete(); } }); }); }
about 4 years ago · Juan Pablo Isaza 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!