javascript 类型错误:value.push 不是带有 Angularjs $resource 查询的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31458407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: value.push is not a function with Angularjs $resource query
提问by Kaspar
I am returning a array of objects from the server:
我正在从服务器返回一组对象:
[{id: 1, name: "name"},{id: 2, name: "name2"}]
Now I use angular-resource $query
to fetch the data as it expects an array.
When the data is received I get this error:
现在我使用 angular-resource$query
来获取数据,因为它需要一个数组。收到数据后,我收到此错误:
TypeError: value.push is not a function
Is there an issue with the response I give from server=?
我从 server= 给出的响应是否有问题?
Source of error:
错误来源:
// jshint +W018
if (action.isArray) {
value.length = 0;
forEach(data, function(item) {
if (typeof item === "object") {
value.push(new Resource(item));
} else {
// Valid JSON values may be string literals, and these should not be converted
// into objects. These items will not have access to the Resource prototype
// methods, but unfortunately there
value.push(item);
}
});
} else {
shallowClearAndCopy(data, value);
value.$promise = promise;
}
}
Controller:
控制器:
var stream = [];
stream = new VideoStream({param: 'streamTypes'});
stream.$query();
Service:
服务:
app.service('DataService', [
'$resource', 'Common', '$rootScope',
function($resource, Common, $rootScope) {
return $resource($rootScope.appWebRoot + "myUrl/:param", {param: '@param'},
{
});
}
]);
VideoStream:
视频流:
app.service('VideoStream', [
'$resource', 'Common', '$rootScope',
function($resource, Common, $rootScope) {
return $resource($rootScope.appWebRoot + "videoStreams/api/:param",
{param: '@param'},
{
});
}
]);
回答by Wawy
The problem you have is that you are creating an instance of your resource as an object
您遇到的问题是您正在创建资源的实例作为对象
var stream = [];
stream = new VideoStream({param: 'streamTypes'}); //This is the problem. $resource is expecting an array.
stream.$query(); //This is an instance method.
//All you need to do is:
var stream = [];
stream = VideoStream({param: 'streamTypes'}).query();
From https://docs.angularjs.org/api/ngResource/service/$resource:
从https://docs.angularjs.org/api/ngResource/service/$resource:
$resource returns:
$resource 返回:
A resource "class" object with methods for the default set of resource actions optionally extended with custom actions. The default set contains these actions:
{ 'get': {method:'GET'}, 'save': {method:'POST'}, 'query': {method:'GET', isArray:true}, 'remove': {method:'DELETE'}, 'delete': {method:'DELETE'} };
Calling these methods invoke an $http with the specified http method, destination and parameters. When the data is returned from the server then the object is an instance of the resource class. The actions save, remove and delete are available on it as methods with the $ prefix
一个资源“类”对象,具有用于默认资源操作集的方法,可选择使用自定义操作进行扩展。默认设置包含以下操作:
{ 'get': {method:'GET'}, 'save': {method:'POST'}, 'query': {method:'GET', isArray:true}, 'remove': {method:'DELETE'}, 'delete': {method:'DELETE'} };
调用这些方法会使用指定的 http 方法、目标和参数调用 $http。当数据从服务器返回时,对象就是资源类的一个实例。操作 save、remove 和 delete 作为带有 $ 前缀的方法可用
回答by NateNjugush
In addition to Wawy's answer, according to the docs here:
The action methods on the class object or instance object can be invoked with the following parameters:
- HTTP GET "class" actions: Resource.action([parameters], [success], error])
- non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
- non-GET instance actions: instance.$action([parameters], [success], [error])
可以使用以下参数调用类对象或实例对象上的操作方法:
- HTTP GET“类”操作:Resource.action([parameters], [success], error])
- 非 GET“类”操作:Resource.action([parameters], postData, [success], [error])
- 非 GET 实例操作: instance.$action([parameters], [success], [error])
On checking the value of VideoStream
in the controller, we find:
在检查VideoStream
控制器中的值时,我们发现:
function Resource(value) {
shallowClearAndCopy(value || {}, this);
} // Actual Resource class object with regular action(s) we can work with
there
那里
Calling VideoStream({param: 'streamTypes'})
returns:
调用VideoStream({param: 'streamTypes'})
返回:
undefined // Cannot call anything of undefined
and new VideoStream({param:'streamTypes'})
returns:
并new VideoStream({param:'streamTypes'})
返回:
Resource {param: "streamTypes"}
// Instance of the Resource class (an object) where $action(s) are available
// Verified in the chrome console
With this in mind, the case should be:
考虑到这一点,情况应该是:
var stream = [];
stream = VideoStream.query({param: 'streamTypes'});