Angular2 Property does not exist on type 'any[]' (JSON)

14,691

Your search method should probably return an object, not an array. 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.

Share:
14,691

Related videos on Youtube

Bünyamin Sarıgül
Author by

Bünyamin Sarıgül

METU Computer Engineering - Thundra.io

Updated on June 19, 2022

Comments

  • Bünyamin Sarıgül
    Bünyamin Sarıgül almost 2 years

    I have a class named Condition:

    export class Condition{
        name: string;
        date: string;
        link: string;
    }
    

    And a function named search that makes http get requests:

    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:

    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?