AngularJS $resource error - TypeError: Object # has no method 'push'

前端 未结 4 945
名媛妹妹
名媛妹妹 2020-12-15 05:20

I have the following in my controller file:

var Subject = $resource(\'/api/TestAccounts/:action\', { applicationId: 3 }, {
   \'getSelect\': { method: \'GET\         


        
相关标签:
4条回答
  • 2020-12-15 05:59

    I just changed MyService.get(...) to MyService.query(...) because .get() expects an object and .query() expects an array.

    0 讨论(0)
  • 2020-12-15 05:59

    You need to set the paramater isArray to true in your resource definition if you are returning an array from the server.

    var Subject = $resource('/api/TestAccounts/:action', { applicationId: 3 }, {
      'getSelect': { method: 'GET', isArray: true, params: { action: 'GetSelect' } }
    });
    

    See: AngularJS $resource API

    0 讨论(0)
  • 2020-12-15 06:17

    By default, GET expects an object {}, QUERY expects an array[]. In your case, Web API returns an array for GET, so you need to set isArray flag for GET as true. If your Web API returned an object, then you would not need to reset that flag because it is GET's default behavior.

    0 讨论(0)
  • 2020-12-15 06:21

    I know this post is a bit stale but I found a solution when I ran into this problem today. Following the post below, I implemented joelwreed's solution (half way down the thread). It does a check that the destination is an array before trying to push, which fixes the issue.

    Link to issue filed on github

    0 讨论(0)
提交回复
热议问题