简体   繁体   English

类型'any []'(JSON)上不存在Angular2属性

[英]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.

相关问题 解决promise angular2:类型“ []”上不存在属性“ includes” - Resolving promise angular2: Property 'includes' does not exist on type '[]' Angular2和Typescript-类型上不存在属性名称 - Angular2 & Typescript - Property name does not exist on type Angular 2属性订阅在Promise类型上不存在<any> - Angular 2 Property subscribe does not exist on type Promise <any> Angular4打字稿; 属性“ data”在类型“ any []”上不存在 - Angular4 Typescript; Property 'data' does not exist on type 'any[]' Angular2 map - &gt; subscribe数据类型错误(类型&#39;ErrorObservable&#39;上不存在属性&#39;length&#39;。) - Angular2 map -> subscribe data type error ( Property 'length' does not exist on type 'ErrorObservable'.) 错误:“&#39;JSON&#39;类型不存在属性&#39;.get&#39;(HTML / Javascript / Angular2 / Typescript) - Error: "Property '.get' doesn't exist on type 'JSON' (HTML / Javascript / Angular2 / Typescript) 使用Ionic2和Angular2的类型&#39;Navigator&#39;上不存在属性&#39;connection&#39; - Property 'connection' does not exist on type 'Navigator' using Ionic2 and Angular2 Angular2打字稿错误TS2339:类型“ Y”上不存在属性“ x” - Angular2 typescript error TS2339: Property 'x' does not exist on type 'Y' angular2 组件内的 iframe,“HTMLElement”类型上不存在属性“contentWindow” - iframe inside angular2 component, Property 'contentWindow' does not exist on type 'HTMLElement' 在“OperatorFunction”类型上不存在获取属性“过滤器”<any, any> ' Angular 中的错误</any,> - Getting a Property 'filter' does not exist on type 'OperatorFunction<any, any>' error in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM