Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular.js non-GET instance action returns error: value.push is not a function

    var user = $resource('/user/:action',{},{
        create: {method:'POST',isArray:true, params:{action:'save'}},
    });
    user.$create(function(userData) {
    //do something          
    });

The HTTP result code is 200, and proper data is returned for the $create. The 'on success' function call fails with the error below:

Error: value.push is not a function
  resourceFactory/</Resource[name]/promise</<@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular-resource.js:532

etc.

The returned JSON from $create IS an array. If I remove the function call from $create(..) and make it $create() - it works fine.

I guess(?) that this has to do with Array vs. Object, but as I said - I know the result IS an array JSON, and (well) I tried both permutations..

Edit: I think that Angular has a problem with how the response format. (?) It's an array with (sometimes) one element. Is this format incorrect?

[{
     "hashcode": "0",
     "object-type": "aa",
}]

Thanks

like image 694
JRun Avatar asked Jun 10 '26 15:06

JRun


2 Answers

I came across the same issue, when using the instance-post-approach:

// Resource definition
var MyResource = $resource('/api/myresource/:id', 
  { id: '@id' },
  { save: {method: 'POST', isArray: true} }
);
// ...
var myInstance = new MyResource();
myInstance.$save(attributes).then(...);  // Expects result to be object

However, using the non-instance-based method, the result of the post is returned properly in an array:

// Resource definition as above ...
MyResource.save(attributes).$promise.then(...);  // Expects result to be array
like image 127
tokosh Avatar answered Jun 13 '26 03:06

tokosh


I had the same issue and it seems that $resource object, when it makes a post request can't accept an array in response or that may be a bug. Anyways, I was able to work this around by converting the array response from the server to a json response. so in your case a server response, something like this should work -->

{ "response" : [{
     "hashcode": "0",
     "object-type": "aa",
}]}
like image 39
maurya8888 Avatar answered Jun 13 '26 04:06

maurya8888



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!