In my Angular 2 project ListingService can't get data from server via post. I'm getting this error:
EXCEPTION: TypeError: Cannot read property 'post' of undefined in [null] ORIGINAL EXCEPTION: TypeError: Cannot read property 'post' of undefined ORIGINAL STACKTRACE: TypeError: Cannot read property 'post' of undefined at ListingService.getListings (http://localhost:3000/app/listing.service.js:30:30) at ListingsComponent.getListings (http://localhost:3000/app/listings.component.js:45:41) at ListingsComponent.ngOnInit (http://localhost:3000/app/listings.component.js:41:26) at AbstractChangeDetector.ChangeDetector_HostListingsComponent_0.detectChangesInRecordsInternal (eval at (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:10897:14), :22:99) at AbstractChangeDetector.detectChangesInRecords (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8824:14) at AbstractChangeDetector.runDetectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8807:12) at AbstractChangeDetector._detectChangesContentChildren (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8871:14) at AbstractChangeDetector.runDetectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8808:12) at AbstractChangeDetector._detectChangesInViewChildren (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8877:14) at AbstractChangeDetector.runDetectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8811:12)
My ListingService.ts looks like this:
import {Injectable, Injector} from 'angular2/core'; import {HTTP_PROVIDERS, Http, Headers} from 'angular2/http'; import {Listing} from './listing'; import 'rxjs/add/operator/map'; @Injectable() export class ListingService { http: Http; listings: Array; getListings() { var headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.post('http://144.376.29.134:and-so-on', JSON.stringify( {"id":1, "title": "Title", "description": "Проводите", "discount": "21%", "imageUrl": "http://lorempixel.com/400/200/sports", "tags": [{"tag": "Еда"}] }),{headers:headers} ).map(res => res.json()) .subscribe((res:Array) => this.listings = res); return Promise.resolve(this.listings); } getListing(id: number) { return Promise.resolve(this.listings) .then(listings => listings.filter(l => l.id === id)[0]); } }
And the ListingsComponent, which uses ListingService, looks like this:
import {Component, OnInit} from 'angular2/core'; import {HTTP_PROVIDERS, Http, Headers} from 'angular2/http'; import {Listing} from './listing'; import {ListingService} from './listing.service'; import 'rxjs/add/operator/map'; @Component({ .... }) export class ListingsComponent implements OnInit { listings: Array; constructor(private listingService:ListingService, private _router: Router) {} ngOnInit() { this.getListings(); } getListings() { this.listingService.getListings().then(listings => this.listings = listings); } getListing(id: number) { return Promise.resolve(this.listings) .then(listings => listings.filter(l => l.id === id)[0]); } gotoDetail(listing: Listing) { this._router.navigate(['ListingDetail', { id: listing.id }]); } }
What can be problem of this?