[英]Angular2 Property does not exist on type 'any[]' (JSON)
I have a class named Condition : 我有一个名为Condition的类:
export class Condition{
name: string;
date: string;
link: string;
}
And a function named search that makes http get requests: 以及一个名为search的函数,它使http获取请求:
public search(params: string): Observable<json[]> {
let queryString = this.serverUrl + params;
return this.http.get(queryString)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
And I get an object to get conditions with specific properties from it. 我得到一个对象来获取具有特定属性的条件。 It has absolutely an object array in its entry attribute:
它的entry属性中绝对有一个对象数组:
this.search(`Condition?patient=Patient/107795`).subscribe(data => {
var conditionArray = data.entry; // data.entry is absolutely an ARRAY
for(var condition of conditionArray){
conditions.push({ //conditions is a Condition[]
name: condition.resource.code.coding[0].display,
date: condition.resource.dateRecorded,
link: "condition/" + condition.resource.id
});
}
} , err => { console.log(err); } );
I was started these project before I wrote these code with start script: 在使用启动脚本编写这些代码之前,我启动了这些项目:
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
And the project was working as I wanted and no error on the console. 该项目按我的意愿工作,并且在控制台上没有错误。 However, when I stopped it and started again with the start script, it gives this error:
但是,当我停止它并使用启动脚本再次启动时,它会出现以下错误:
error TS2339: Property 'entry' does not exist on type 'any[]'.
Do you know what is wrong with it and how can I fix it? 你知道它有什么问题吗?我该如何解决?
Your search
method should probably return an object, not an array. 您的
search
方法应该返回一个对象,而不是一个数组。 Declare it as follows: 声明如下:
public search(params: string): Observable<any> {
Use any
unless you have a type describing the result, if so, you may use that instead. 除非您有描述结果的类型,否则请使用
any
,如果有,您可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.