Red Virus
Red Virus

Reputation: 1707

EXCEPTION: Error in :0:0 caused by: this.http is not a function

I was following a YouTube tutorial and got into a weird error "EXCEPTION: Error in :0:0 caused by: this.http is not a function". I have triple checked my code and was exactly like in the tutorial video.

Please help, I am just 2 days old in Ionic framework.

src/app/service.ts

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/Rx';

@Injectable()
export class ExampleApis{

    http: any;
    requestURL: string;

    constructor( http:Http ){
        this.http = http;
        this.requestURL = 'https://www.example.com/api';
    }

    list(){
        var object = this.http( this.requestURL + '/?format=json' ).map( result => result.json() );
        return object;
    }

}

home.ts

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { ExampleApis } from '../../app/service';

@Component({
  selector: 'page-places-to-stay',
  templateUrl: 'places-to-stay.html'
})
export class PlacesToStayPage {

  constructor(public navCtrl: NavController, public navParams: NavParams, private ExampleApis:ExampleApis) {}

  ngOnInit(){
    this.list();
  }

  list(){
   this.ExampleApis.list().subscribe( response => {
     console.log( response );
   });
  }

}

Upvotes: 1

Views: 123

Answers (1)

Navneil Naicker
Navneil Naicker

Reputation: 3691

Change this method

list(){
        var object = this.http( this.requestURL + '/?format=json' ).map( result => result.json() );
        return object;
    }

to this

list(){
        var object = this.http.get( this.requestURL + '/?format=json' ).map( result => result.json() );
        return object;
    }

What was wrong? You forgot the .get on http

Upvotes: 1

Related Questions