This.http.post is not a function error


#1

Hi,

I’m having an error regarding http in my service class. I’ve already imported Http from @angular/http as well as rxjs/Rx and rxjs/add/operatror/map but still the error persists. Can someone please help? It will be much appreciated. TIA. Posted is my service code

import { Http } from ‘@angular/http’;
import ‘rxjs/Rx’;
import ‘rxjs/add/operator/map’;

export class RestService {

url: string;
data: any;

static get parameters(){
	return [[Http]];
}

constructor(public http: Http) {
	 
	this.url = "<my backend url here>";
	this.data = {};
	this.data.response = "";
	this.http = http;
}

doLogin(params) {
	
	var loginUrl = this.url + "login";
	
	var data = JSON.stringify({ 
		email : params.usernameOrEmail, 
		password: params.password
	});
	
	console.log(loginUrl + "," + data);
	
	this.http.post(loginUrl, data).subscribe(data => {
		this.data.response = data.json()._body;
    },  error => {
        console.log("Oooops!");
    });
}

doRegister(params) {
	var registerUrl = this.url + "signup";
	var data = JSON.stringify({email : params});
	
	this.http.post(registerUrl, data).subscribe(data => {
		this.data.response = data.json()._body;
    },  error => {
        console.log("Oooops!");
    });		
}

doConfirmEmail(params) {
	var confirmEmailUrl = this.url + "confirm/email";
	var data = JSON.stringify({email : params});
	
	this.http.post(confirmEmailUrl, data).subscribe(data => {
		this.data.response = data.json()._body;
    },  error => {
        console.log("Oooops!");
    });
}

}

This is the error

EXCEPTION: Error in ./Login class Login - inline template:20:28 caused by: this.http.post is not a function


#2

Get rid of the parameters getter function. You are mashing up TypeScript and JavaScript syntax. Also there is no need to assign to http in the constructor, and that is also probably causing problems.


#3

Hi,

Thanks for the reply. I did removed the getParameters as well as the assignment of http in the constructor but still no luck? Any ideas?


#4

Hello, anyone there?


#5

This issue was fixed now. I have to declare the service class as providers in app.module.ts