0

Iam using Angular 11, use rxjs and i wont filter my course collection to course with category equal "frontend". but i got some error

My Code :

getPosts():Observable<ICourses[]> {
    return this.http.get<ICourses[]>(`${apiURL}course/getall`).pipe(
      map((courses: ICourses[]) => {
          return Object.values(courses).filter(course => {
            course.devCategory = "frontend"
          })
      })
    )
  }

Error :

ERROR TypeError: Cannot create property 'devCategory' on boolean 'true'

Interface ICourses :

interface ICourses {
   _id: string
   title: string,
   image: string,
   instructor: string,
   topic: string,
   level: string,
   price: number,
   hours: number,
   students: number,
   category: string,
   devCategory: string,
   created_at: Date,
   updated_at: Date
   date: string
}

export {ICourses}

If i use this code :

getPosts():Observable<ICourses[]> {
    return this.http.get<ICourses[]>(`${apiURL}course/getall`).pipe(
      map((courses: ICourses[]) => {
          **return courses.filter(course => {
            course.devCategory = "frontend"
          })**
      })
    )
  }

I got this error :

ERROR TypeError: courses.filter is not a function
5
  • have a look to your ICourses interface, it looks like you did something like devCategory: true instead of devCategory: string Commented May 15, 2021 at 16:38
  • Show interface for "Category". It looks like devCatergory property defined there as boolean Commented May 15, 2021 at 16:38
  • 1
    course => course.devCategory === "frontend" the solution for your filter. Commented May 15, 2021 at 18:44
  • what values courses have? Commented May 15, 2021 at 18:52
  • It looks to me, that your course is not ICourses but boolean. Commented May 15, 2021 at 19:17

2 Answers 2

3

This error

ERROR TypeError: Cannot create property 'devCategory' on boolean 'true' 

says that this line

course.devCategory = "frontend"

tries to do literally

 true.devCategory="frontend"

which is obviously not what you want to do.

double check what actually is returned in HTTP response as it looks like it is not ICourses[] at all

Sign up to request clarification or add additional context in comments.

Comments

1

First of all: For filter to work you need to return a boolean value. Also if courses already is an array you won't need Object.values to filter it:

getPosts(): Observable<ICourses[]> {
  return this.http
    .get<ICourses[]>(`${apiURL}course/getall`)
    .pipe(
      map((courses: ICourses[]) => {
        return courses.filter((course) => course.devCategory === "frontend")
      })
    )
}

But you seem to have a problem with your API, because the courses seem to bee boolean values not ICourse objects.

3 Comments

i got this error : ERROR TypeError: courses.filter is not a function
Yes because your API returns wrong values
What does this log in the console .pipe(tap(console.log))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.