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

249
Views
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 answers
Answer question

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 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!