Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

angular - TypeError: Cannot read property 'post' of undefined in [null]

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 <anonymous> (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:10897:14), <anonymous>: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<Listing>;
 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<Listing>) => 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<Listing>;
 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to add HTTP_PROVIDERS to either the component providers array like this:

providers: [HTTP_PROVIDERS]

or preferably in the bootstrap like this:

bootstrap(AppComponent, [HTTP_PROVIDERS]);

And you are missing the constructor http injection in the ListingService:

export class ListingService {
   constructor(private http : Http){}
}

addendum

The reason you are not receiving any listings is because you are using a Promise instead of Observable:

in getListings() in ListingService return this:

return this.http.post("bla bla").map(res => res.json).map((res) => {
    return this.listings = res;
});

then subscribe to this in the getListings() in the ListingsComponent:

getListings() {
  this.listingService.getListings().subscribe(listings => this.listings = listings);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...