【发布时间】:2017-05-02 10:37:38
【问题描述】:
所以我刚开始学习 Angular2,我试图获取我在数据库中的数据并将其显示在我的索引页上,但每次我尝试这样做时,我都会收到一条错误消息“无法读取未定义的属性 'map'”和我不知道为什么我一直在寻找几个小时。谁能帮帮我。
import { Component } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Injectable } from '@angular/core';
import { Bootcamp } from './Bootcamp';
const BOOTCAMP: Bootcamp[] = [];
@Injectable()
export class Bootcamptest {
private baseUrl: string = 'http://localhost:54220/Bootcampdata';
constructor(private http: Http) { }
getAll(): Observable<Bootcamp[]> {
let bootcamp$ = this.http
.get(`${this.baseUrl}/GetAlleBootcamps`, { headers: this.getHeaders() })
.map(mapBootcamps)
.catch(handleError);
return bootcamp$;
}
private getHeaders() {
let headers = new Headers();
headers.append('Accept', 'application/json');
return headers;
}
}
function mapBootcamps(response: Response): Bootcamp[] {
// uncomment to simulate error:
// throw new Error('ups! Force choke!');
// The response of the API has a results
// property with the actual results
return response.json().results.map(toBootcamp)
}
function toBootcamp(r: any): Bootcamp {
let bootcamp = <Bootcamp>({
id: extractId(r),
naam: r.name,
description: r.description,
begindatum: r.begindatum,
einddatum: r.einddatum
});
console.log('Parsed bootcamp:', bootcamp);
return bootcamp;
}
// to avoid breaking the rest of our app
// I extract the id from the person url
function extractId(bootcampData: any) {
let extractedId = bootcampData.url.replace('http://localhost:54220/Bootcampdata/Getallebootcamps', '').replace('/', '');
return parseInt(extractedId);
}
function mapBootcamp(response: Response): Bootcamp {
// toBootcamp looks just like in the previous example
return toBootcamp(response.json());
}
// this could also be a private method of the component class
function handleError(error: any) {
// log error
// could be something more sofisticated
let errorMsg = error.message || `Error met het laden van data!`
console.error(errorMsg);
// throw an application level error
return Observable.throw(errorMsg);
}
组件类
import { Component, OnInit } from '@angular/core';
import { Bootcamp } from './Bootcamp';
import { Bootcamptest } from './Bootcamptest';
@Component({
selector: 'my-bootcamptest',
template: `
<section>
<section *ngIf="isLoading && !errorMessage">
Loading our hyperdrives!!! Retrieving data...
</section>
<ul>
<!-- this is the new syntax for ng-repeat -->
<li *ngFor="let person of people">
<a>
{{bootcamp.naam}}
</a>
</li>
</ul>
<section *ngIf="errorMessage">
{{errorMessage}}
</section>
</section>
`
})
export class Bootcamplistcomponent implements OnInit {
bootcamp: Bootcamp[] = [];
errorMessage: string = '';
isLoading: boolean = true;
constructor(private bootcamptest: Bootcamptest) { }
ngOnInit() {
this.bootcamptest
.getAll()
.subscribe(
/* happy path */ p => this.bootcamp = p,
/* error path */ e => this.errorMessage = e,
/* onComplete */() => this.isLoading = false);
}
}
【问题讨论】:
-
错误点在哪里?它应该至少给你一个文件或发生它的一行。它似乎不是来自
getAll()方法,因为它对我来说似乎很好...... -
在
mapBootcamps函数中检查response.json()的内容。里面好像没有results属性 -
好像
.get('${this.baseUrl}/GetAlleBootcamps', { headers: this.getHeaders() })为空……只要没有DI异常,就很难找到问题了。 -
response.json() 的内容是一个数组,其中包含来自我的数据库的正确数据,所以我认为这不是问题。当我执行 console.log(response.json()) 时,我可以看到其中的正确数据。难道是我使用的日期格式导致了问题?
-
这是 console.log 的样子 Array[1] 0 : Object Begindatum : "/Date(1448146800000)/" 描述 : "test data" Einddatum : "/Date(1450738800000)/" IdBootcamp:1 Naam:“Project1”proto:对象长度:1 proto:Array[0]
标签: angular