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

251
Views
Error en el servicio observable sin estado: el tipo 'Observable<Curso[]>' no se puede asignar al tipo 'Observable<Curso>'

Estoy aprendiendo rxjs y me quedé atascado con este error.

tengo el siguiente metodo

 getCourseById(id: number): Observable<Course> { return this.http.get<Course[]>('the-api').pipe( map(courses => courses.filter(course => course.id === id) ) ) }

Hago una llamada a un punto final de API en particular, tomo la respuesta completa, luego uso el mapa para filtrar contra esa colección y obtengo el curso con una identificación igual a la identificación del parámetro.

La función devuelve un Observable de tipo Curso.

Sin embargo, en la consola veo:

 error TS2322: Type 'Observable<Course[]>' is not assignable to type 'Observable<Course>'. Type 'Course[]' is missing the following properties from type 'Course': id, name, url, description 27 return this.http.get<Course[]>('the-api').pipe( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 28 map(courses => ~~~~~~~~~~~~~~~~~~~~~ ... 30 ) ~~~~~~~~~ 31 ) ~~~~~

No sé cómo arreglarlo. Agradecería ayuda. Gracias

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

0

Debe usar Array.find() en lugar de Array.filter() . Esto solo devolverá el objeto coincidente:

 getCourseById(id: number): Observable<Course> { return this.http.get<Course[]>('the-api').pipe( map(courses => courses.find(course => course.id === id)! ) ) }
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!