3

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());
    });
  }
6
  • Which version of rxjs/angular u r using Commented May 25, 2018 at 12:30
  • what is the version of RXJS ? Commented May 25, 2018 at 12:30
  • 5.5.10 is the version of my rxjs. I've just checked it. Commented May 25, 2018 at 12:32
  • Why are you using HttpModule it is deprecated you should use HttpClientModule Commented May 25, 2018 at 12:46
  • @Vikas where has the OP mentioned about using HttpModule? you are just assuming I guess ... Commented May 25, 2018 at 13:36

3 Answers 3

6

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))
             );
Sign up to request clarification or add additional context in comments.

3 Comments

I'm using import { map } from 'rxjs/operators'; right now. That's not the same?
^ this is version 6+ way of importing
It works well with import { map } from 'rxjs/operators'; at me with rxjs version 5.5.10... By the way the problem was I didn't call map inside the pipe function. Thanks a lot! ;)
0
Using Observable.subscribe directly should work.like below

@Injectable()
export class HallService {
    public http:Http;
    public static PATH:string = 'app/backend/'    

    constructor(http:Http) {
        this.http=http;
    }

    getHalls() {
    // ########### No map
           return this.http.get(HallService.PATH + 'hall.json');
    }
}


export class HallListComponent implements OnInit {
    public halls:Hall[];
    / *** /
    ngOnInit() {
        this._service.getHalls()
           .subscribe(halls => this.halls = halls.json()); // <<--
    }
}

Comments

0

I had the problem too, when using somearray.map(...), and the problem turned out to be that somearray was null cause it had not been imported from the backend properly.

Comments

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.