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</<@.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
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./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
Share Improve this question edited Mar 21, 2014 at 12:10 JRun asked Mar 20, 2014 at 15:02 JRunJRun 3,4286 gold badges28 silver badges41 bronze badges 6- Where do you use the variable "value"? – Sunil D. Commented Mar 20, 2014 at 15:14
- 1 nowhere. I think it's something in Angular – JRun Commented Mar 20, 2014 at 15:20
- Is user0 a typo? Btw. funny how you asked a question about value.push, yet you never show it in your code. – Shomz Commented Mar 21, 2014 at 9:43
- @Shomz Yes it's a typo, and yes it's funny - but it es from Angular code not mine :-) – JRun Commented Mar 21, 2014 at 12:09
- 1 this is a class-action, not an instance-action. – Eliran Malka Commented Mar 21, 2014 at 20:53
3 Answers
Reset to default 8I 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
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",
}]}
You're misusing the API: the only required parameter for non-GET class actions is the postData
object. From the docs:
The action methods on the class object or instance object can be invoked with the following parameters:
[…]
- non-GET "class" actions:
Resource.action([parameters], postData, [success], [error])
… but you're only passing a callback:
user.$create(function(userData) {
//do something
});
Pass a data object as the first argument, and it should be fine:
user.$create( {} , function(userData) {
//do something
});