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());
});
}
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))
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With