Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
Error in stateless observable service: Type 'Observable<Course[]>' is not assignable to type 'Observable<Course>'

I'm learning rxjs and I got stuck with this error.

I have the following method

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

I make a call to a particular API endpoint, grab the entire response, then use map to filter against that collection and get the course with id equals to the parameter id.

The function returns an Observable of type Course.

Yet, in the console I see:

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     )
    ~~~~~

I don't know how to fix it. I'd appreciate help. Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should use Array.find() instead of Array.filter(). This will only return the matching object:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda