Here is the snippet of code in my component file:
import { Component, Injectable, Inject, OnInit, OnDestroy, EventEmitter, Output } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/switchMap';
...
@Injectable()
export class MyComponent {
constructor(private http: Http) {}
saveDetails() {
var response: any;
var body = customer_details;
var headers = new Headers({ 'Content-Type': 'application/json' });
var options = new RequestOptions({ headers: headers });
this.http.post("/user/add-customer-details", body, options)
.map((res:Response) => res.json()).subscribe(
data => { response = data },
err => console.error(err),
() => {
console.log(response);
}
);
}
}
Despite no compilation error or any other issue, when invoking the post method, I encounter an error stating
Cannot read property 'post' of undefined
. To troubleshoot, I inserted console.log(this.http);
just before the post method call and found it to be displaying undefined
in the browser console. Even after attempting various solutions, I am unable to ascertain why it isn't functioning correctly. Any assistance would be greatly appreciated.
Many thanks for your help!