Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switchMap is not a function

I have followed tutorial Angular Tour of Heroes and some other, now I'm trying to build apps with Angular 2. Basically when we're listening to change with Subject, we can wait for some seconds so that request won't burden the network.

Here is my code :

import { Injectable } from '@angular/core';
import {Http} from "@angular/http";
import {Observable} from "rxjs/Observable";
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
import {Employee} from "../employee";

@Injectable()
export class EmployeeSearchService {

  private getPersonalURL:string = 'api/employee';   // I'm using mock, btw

  constructor(private http:Http) { }

  search(term:string): Observable<Employee[]> {
    return this.http.get(`api/employee/?firstName=${term}`)
      .map(response =>  response.json().data as Employee[])
      .catch(this.handleError);
  }

  searchEmployees(term: Observable<string>): Observable<Employee[]> {
    return term.debounceTime(200)
       .distinctUntilChanged()
       .switchMap(term => this.search(term)); // here is the error
  }

  private handleError(error: any): Promise<any> {
    console.error('Error on EmployeeSearchService', error);
    return Promise.reject(error.message || error);
  }

}

And this is the calling component :

import { Component, OnInit } from '@angular/core';
import {Subject} from "rxjs/Subject";
import { EmployeeService } from '../employee.service';
import { Employee } from '../employee'
import {EmployeeSharedService} from "../employee-shared.service";
import {EmployeeSearchService} from "../employee-search/employee-search.service";

@Component({
  selector: 'employee-list',
  providers: [ EmployeeService ],
  templateUrl: './employee-list.component.html',
  styleUrls: ['./employee-list.component.css']
})
export class EmployeeListComponent implements OnInit {

  employees: Employee[];
  selectedEmployee: Employee;
  sortAsc: boolean;

  searchQuery = new Subject<string>();

  constructor(private employeeService:EmployeeService, private employeeSearchService:EmployeeSearchService, private employeeSharedService:EmployeeSharedService) {
    this.sortAsc = true;

  }

  ngOnInit() {    // called from here
    this.employeeSearchService.searchEmployees(this.searchQuery)
        .subscribe(result => {
          this.employees = result;
          if(!result) {
            this.getAllEmployees();
          }
        });
  }

  getAllEmployees(): void {
      // calling get all in service
  }

}

There are error that says

TypeError: term.debounceTime(...).distinctUntilChanged(...).switchMap is not a function

Am I missing something, like import or other thing? Because exact code and import are same as Angular Tour of Heroes tutorial, and mine works. But this one isn't.

like image 327
imeluntuk Avatar asked Apr 23 '17 09:04

imeluntuk


2 Answers

You also need to add the switchMap operator:

import 'rxjs/add/operator/switchMap';
like image 177
Poul Kruijt Avatar answered Oct 09 '22 17:10

Poul Kruijt


Here is the culprit searchQuery = new Subject<string>();

A Subject is not an observable, but your function is asking for one, so modify it like this

ngOnInit() {
    this.employeeSearchService.searchEmployees(this.searchQuery.asObservable())...
}
like image 34
Meme Composer Avatar answered Oct 09 '22 15:10

Meme Composer