首页 > 解决方案 > Getting .apply is not a function

问题描述

does anyone know why I'm getting:

ERROR TypeError: _this.user_data.apply is not a function

While tying to populate user_data with a mapped result from a JSON file

user_data: any;

this.testService.getTestWithObservable()
 .map( result => result.map( i => i.users.data) )
 .subscribe(
    res => {

        this.user_data = [];
        res.forEach(i => this.user_data(...i));
        console.log(this.user_data);
    }
);
}

JSON

[{
    "id": 1,
    "users": {
        "user_id": 14,
        "data": [{
            "name": "James",
            "age": 20
        },
        {
            "name": "Damien",
            "age": 25
        }]
    }
}
{
    "id": 2,
    "users": {
        "user_id": 11,
        "data": [{
            "name": "James",
            "age": 20
        },
        {
            "name": "Damien",
            "age": 25
        }]
    }
}]

I can't find anything related to this error/code

标签: jsonangulartypescript

解决方案


解决了:

TS

       res => {
           this.user_data= res;
       }

HTML

   <div *ngFor="let data of user_data">
       <div *ngFor="let x of data">
        {{ x.name}}
       </div>
   </div>

推荐阅读