bier hier bier hier - 2 years ago 176
AngularJS Question

Angular 2: how to fix 'cannot read property "lastIndexOf"?

Trying to get my head around angular 2 and created a plunker which injects a service:

import {Injectable} from 'angular2/core';
import {URLSearchParams, Jsonp,Http} from 'angular2/http';

export class WikipediaService {
constructor(private jsonp: Jsonp) {}

search (term: string) {
var search = new URLSearchParams()
search.set('action', 'opensearch');
search.set('search', term);
search.set('format', 'json');
return this.jsonp
.get('', { search })
.map((request) => request.json()[1]);

This service is being used in another class called FriendsList:

import {Component,View,CORE_DIRECTIVES} from 'angular2/core';
import {Http, Response,HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/Rx';
import {WikipediaService} from 'src/service',
import {Control} from 'angular2/common';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
import {Observable} from 'rxjs/Observable';

template: `
<h2>Wikipedia Search</h2>
<input type="text" [ngFormControl]="term"/>
<li *ngFor="#item of items | async">{{item}}</li>

export class FriendsList{
items: Observable<Array<string>>;
term = new Control();

constructor(private wikipediaService: WikipediaService) {
this.items = this.term.valueChanges
.switchMap(term =>;


The error in the console is like this, how can I fix this? :

Error: Cannot read property 'lastIndexOf' of undefined
Error loading as "src/myfriends" from
at l.k (
at l.normalize (
at l.<anonymous> (
at l.<anonymous> (
at l.<anonymous> (
at l.<anonymous> (
at l.instantiate (
at (
at zoneBoundFn (


Answer Source

You have a typo in your myfriends.ts file:

import {WikipediaService} from 'src/service',

You need to use ; instead of , like this:

import {WikipediaService} from 'src/service';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download