i am new one so difficult to getting this error using resource module after calling service. can any one modify my mistake in code where i am getting wrong or just modified that pieace of which needs to rectify thanx must be appreciated.
在调用服务之后,我很难使用资源模块来获取这个错误。有没有人可以修改我的错误,在代码中,我正在出错,或者只是修改了那需要修正的部分,谢谢。
Format Of data Coming:-
数据格式:-
[
brands: Array[1]
0: Object
__v: 0
_id: "5251a4a34f232fc3902"
account_id: "525072320e32971b"
name: "Fruits"
__proto__: Object
1: Object
length: 2
categories: Array[1]
0: Object
__v: 0
_id: "5251a4a34f2323fc3902"
account_id: "5250723230e32971b"
name: "Fruits"
__proto__: Object
1: Object
length: 2
]
Error:-
错误:-
[$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object
[$resource:badcfg]资源配置错误。包含数组但得到对象的预期响应
itmsFormView.html
itmsFormView.html
<select class="form-control" ng-model="item.brand_id" id="itemBrand" >
<option value="">Select Brand</option>
<option ng-repeat="brand in brands" value="{{brand.brand_id}}">{{brand.name}} </option>
</select>
<select class="form-control" ng-model="item.category_id" id="itemCategory">
<option value="">Select Category</option>
<option ng-repeat="category in categories" value="{{category.brand_id}}">{{category.name}}</option>
</select>
ItemsService.js
ItemsService.js
app.factory('itemService', function ($resource) {
return {
getCategoryAndBrand : $resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})
};
});
ItemsController.js
ItemsController.js
app.controller('itemsFormController', function ($rootScope, $scope, itemService, $location, $cookies, $routeParams) {
itemService.getCategoryAndBrand.TypeGetCategoryAndBrand({}, function(response){
console.log(response);
},function(errorResponse){
console.log(errorResponse);
}
);
});
5 个解决方案
#1
16
From the documentation:
从文档:
Action isArray
行动isArray
isArray – {boolean=} – If true then the returned object for this action is an array, see returns section.
isArray - {boolean=} -如果为真,则此操作的返回对象是一个数组,请参见返回部分。
According to your code isArray = true. Set it to false and you can use an object instead of an array.
根据您的代码isArray = true。将其设置为false,您可以使用对象而不是数组。
app.factory('itemService', function ($resource) {
return {
getCategoryAndBrand : $resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})
};
});
It's expecting you to pass in the parameters as an array not an object
它期望您将参数作为数组传递,而不是对象
from your code
从你的代码
$resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})
$resource(“/user/ classies_brands”,{},{TypeGetCategoryAndBrand:{method: 'get', isArray:true})
If the documentation is to be believed than with the error your getting I'd try to pass this in the form of an array and not an object to see if you get the same response.
如果文档是可信的,而不是错误的,我将尝试以数组的形式传递它,而不是一个对象来查看是否得到相同的响应。
#2
7
In my case, the API server was returning an HTML page (an error page) that is treated as a string, instead of the correct JSON response which would be a javascript object.
在我的示例中,API服务器返回一个HTML页面(一个错误页面),该页面被视为字符串,而不是正确的JSON响应,即javascript对象。
As a sidenote, javascript errors and stacktraces are really something.
作为sidenote, javascript错误和堆栈跟踪实际上是一回事。
#3
4
Hope this answer is not too late.
希望这个答案不会太迟。
You should not fix this problem by putting a isArray:true statement to override "get" method's nature in your resource declaration.
您不应该通过在资源声明中放置isArray:true语句来覆盖“get”方法的性质来解决这个问题。
There are two ways to fix this problem.
有两种方法可以解决这个问题。
1.Use "query" method instead. It works like "get" method totally, but its isArray is set to true naturally.
1。使用“查询”的方法。它的工作方式完全类似于“get”方法,但是它的isArray设置为true。
TypeGetCategoryAndBrand:{method: 'query'}
2.On the server side, responding an object is a much easier and much smarter way. You can continue using "get" method on your client side and change your server side code from this:
2。在服务器端,响应对象是一种更简单、更智能的方式。您可以继续在客户端使用“get”方法,并从以下内容更改服务器端代码:
res.jsonp(articles);
where articles is the result array, to this:
其中冠词为结果数组,为此:
res.jsonp(articles[0]);
#4
0
You are getting an object that contain two arrays.
您将获得一个包含两个数组的对象。
My guess is that in your code before you started with the console.log
outputs, you were setting categories = response;
.
我的猜测是在您开始使用控制台之前的代码中。日志输出,您正在设置categories = response;
You need to set it to: categories = response.categories;
您需要将其设置为:categories = response.categories;
Here:
在这里:
app.controller('itemsFormController', function ($rootScope, $scope, itemService, $location, $cookies, $routeParams) {
itemService.getCategoryAndBrand.TypeGetCategoryAndBrand({}, function(response){
categories = response.categories;
console.log(response);
},function(errorResponse){
console.log(errorResponse);
}
);
});
This might help to understand the problem, also:
这可能有助于理解这个问题,而且:
$resource query返回带有函数的数组,这在迭代数据时不太合适
#5
0
A very simple solve for me from the documentation:
从文档中我得到了一个非常简单的解决方案:
app.factory('Article', ['$resource', function ($resource) {
return $resource('/v1/a/:id', null,
{
'query': {method:'GET', isArray:false},
'update': { method:'PUT' }
});
}]);
#1
16
From the documentation:
从文档:
Action isArray
行动isArray
isArray – {boolean=} – If true then the returned object for this action is an array, see returns section.
isArray - {boolean=} -如果为真,则此操作的返回对象是一个数组,请参见返回部分。
According to your code isArray = true. Set it to false and you can use an object instead of an array.
根据您的代码isArray = true。将其设置为false,您可以使用对象而不是数组。
app.factory('itemService', function ($resource) {
return {
getCategoryAndBrand : $resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})
};
});
It's expecting you to pass in the parameters as an array not an object
它期望您将参数作为数组传递,而不是对象
from your code
从你的代码
$resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})
$resource(“/user/ classies_brands”,{},{TypeGetCategoryAndBrand:{method: 'get', isArray:true})
If the documentation is to be believed than with the error your getting I'd try to pass this in the form of an array and not an object to see if you get the same response.
如果文档是可信的,而不是错误的,我将尝试以数组的形式传递它,而不是一个对象来查看是否得到相同的响应。
#2
7
In my case, the API server was returning an HTML page (an error page) that is treated as a string, instead of the correct JSON response which would be a javascript object.
在我的示例中,API服务器返回一个HTML页面(一个错误页面),该页面被视为字符串,而不是正确的JSON响应,即javascript对象。
As a sidenote, javascript errors and stacktraces are really something.
作为sidenote, javascript错误和堆栈跟踪实际上是一回事。
#3
4
Hope this answer is not too late.
希望这个答案不会太迟。
You should not fix this problem by putting a isArray:true statement to override "get" method's nature in your resource declaration.
您不应该通过在资源声明中放置isArray:true语句来覆盖“get”方法的性质来解决这个问题。
There are two ways to fix this problem.
有两种方法可以解决这个问题。
1.Use "query" method instead. It works like "get" method totally, but its isArray is set to true naturally.
1。使用“查询”的方法。它的工作方式完全类似于“get”方法,但是它的isArray设置为true。
TypeGetCategoryAndBrand:{method: 'query'}
2.On the server side, responding an object is a much easier and much smarter way. You can continue using "get" method on your client side and change your server side code from this:
2。在服务器端,响应对象是一种更简单、更智能的方式。您可以继续在客户端使用“get”方法,并从以下内容更改服务器端代码:
res.jsonp(articles);
where articles is the result array, to this:
其中冠词为结果数组,为此:
res.jsonp(articles[0]);
#4
0
You are getting an object that contain two arrays.
您将获得一个包含两个数组的对象。
My guess is that in your code before you started with the console.log
outputs, you were setting categories = response;
.
我的猜测是在您开始使用控制台之前的代码中。日志输出,您正在设置categories = response;
You need to set it to: categories = response.categories;
您需要将其设置为:categories = response.categories;
Here:
在这里:
app.controller('itemsFormController', function ($rootScope, $scope, itemService, $location, $cookies, $routeParams) {
itemService.getCategoryAndBrand.TypeGetCategoryAndBrand({}, function(response){
categories = response.categories;
console.log(response);
},function(errorResponse){
console.log(errorResponse);
}
);
});
This might help to understand the problem, also:
这可能有助于理解这个问题,而且:
$resource query返回带有函数的数组,这在迭代数据时不太合适
#5
0
A very simple solve for me from the documentation:
从文档中我得到了一个非常简单的解决方案:
app.factory('Article', ['$resource', function ($resource) {
return $resource('/v1/a/:id', null,
{
'query': {method:'GET', isArray:false},
'update': { method:'PUT' }
});
}]);