Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - map is not a function

I'm trying to filer an Observable in my angular project, but it says: "... .map is not a function" so, can you explain what's wrong with my code?

tasks.component.ts (map also imported from rxjs/operators)

ngOnInit() {
    this.tasks = this.tasksService.tasks.map(x => x.filter(y => y.isDone == this.completed));
    this.tasksService.getTasks();
  }

And here's my service which provides the Observable:

task.service.ts

export class TasksService {
  private _tasks = new BehaviorSubject<Task[]>([]);
  tasks = this._tasks.asObservable();
  constructor(private http : Http) {
    console.log('Tasks Service is initialized...');
  }

  getTasks() {
    this.http.get(URL).subscribe(x => {
      this._tasks.next(x.json());
    });
  }
like image 612
Drobesz Avatar asked Mar 08 '26 12:03

Drobesz


1 Answers

Import map like this if you are using rxjs version less than 6.

import 'rxjs/add/operator/map';

If you are using rxjs version 6 or greater, then firstly you have to import operator like this:

import { map } from 'rxjs/operators';

Secondly, you have to use pipe before map:

this.tasks = this.tasksService.tasks.pipe(
                 map(x => x.filter(y => y.isDone == this.completed))
             );
like image 178
Faisal Avatar answered Mar 11 '26 02:03

Faisal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!