我正在尝试使用routeparam来过滤REST调用的结果,但是没有返回任何结果,而是收到以下错误:
Error: error:badcfg Response does not match configured parameter
Error in resource configuration for action `get`. Expected response to contain an object but got an array
我试图获得的价值是由articlecategoryid。如果我将我的URL放到像Postman这样的浏览器工具中就可以了。 articlecategoryid根据需要返回的示例URL如下:
https://myrestcall.net/tables/articles/?$filter=(articlecategoryid eq 1)
但是,在Angular中使用它似乎无法解决并产生错误。
以下是此次调用的一些示例REST数据:
[
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D4",
"articletitle": "artilce1",
"articlecategoryid": 1,
"articlesummary": "article 1 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D5",
"articletitle": "artilce2",
"articlecategoryid": 1,
"articlesummary": "article 2 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D6",
"articletitle": "artilce3",
"articlecategoryid": 1,
"articlesummary": "article 3 summary. "
},
]
这是我的应用设置:
var pfcModule = angular.module('pfcModule', [
'ngRoute',
'ui.bootstrap',
'auth0',
'angular-storage',
'angular-jwt',
'pfcServices',
'pfcControllers']);
pfcModule.config([
'$routeProvider',
'authProvider',
'$httpProvider',
'$locationProvider',
'jwtInterceptorProvider',
function ($routeProvider, authProvider, $httpProvider, $locationProvider, jwtInterceptorProvider) {
$routeProvider.
when('/home', { templateUrl: './views/home.html' }).
when('/categories/:articlecategoryID', { templateUrl: './views/categories.html', controller: 'pfcCategoriesCtrl' }).
when('/article/:articleID/:articleTitle', { templateUrl: './views/article.html', controller: 'pfcCtrl2' }).
when('/add-article', { templateUrl: './views/add-article.html', controller: 'pfcPost', requiresLogin: true }).
when('/login', { templateUrl: './views/login.html', controller: 'loginCtrl' }).
otherwise({ redirectTo: '/home' });
$httpProvider.defaults.headers.common['X-ZUMO-APPLICATION'] = 'myid';
$httpProvider.defaults.headers.common['Content-Type'] = 'Application/json';
authProvider.init({
domain: 'mydomain',
clientID: 'myid',
callbackURL: location.href,
loginUrl: '/login'
});
jwtInterceptorProvider.tokenGetter = function (store) {
return store.get('token');
}
$httpProvider.interceptors.push('jwtInterceptor');
}])
.run(function ($rootScope, auth, store, jwtHelper, $location) {
$rootScope.$on('$locationChangeStart', function () {
if (!auth.isAuthenticated) {
var token = store.get('token');
if (token) {
if (!jwtHelper.isTokenExpired(token)) {
auth.authenticate(store.get('profile'), token);
} else {
$location.path('/login');
}
}
}
});
});
这是我的服务:
var pfcServices = angular.module('pfcServices', ['ngResource'])
pfcServices.factory('pfcArticleCategories', ['$resource', function ($resource) {
return $resource('https://myrestcall.net/tables/articles/?$filter=(articlecategoryid eq :articlecategoryID)', { articlecategoryID: '@articlecategoryid' },
{
'update': { method: 'PATCH' }
}
);
}]);
这是我的控制器:
var pfcControllers = angular.module('pfcControllers', ['auth0']);
pfcControllers.controller('pfcCategoriesCtrl', ['$scope', '$routeParams', 'pfcArticleCategories', function ($scope, $routeParams, pfcArticleCategories) {
$scope.category = pfcArticleCategories.get({ articlecategoryID: $routeParams.articlecategoryID });
}]);
以下是输出REST数据的页面(categories.html):
div class="row">
<div class="col-md-4">
<h2>Heading</h2>
Sort By: <select ng-model="articleSortOrder">
<option value="+id">ID</option>
<option value="+articletitle">Article Title</option>
<option value="+articlecategoryid">Article Category</option>
</select>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Title</th>
<th>Category ID</th>
</tr>
<tr ng-repeat="articles in category | orderBy:articleSortOrder">
<td>{{articles.id}}</td>
<td>{{articles.articletitle}}</td>
<td>{{articles.articlecategoryid}}</td>
</tr>
</table>
</div>
以下是我如何链接到此页面以通过routeparam(home.html)过滤它:
<a href="#categories/1">Article 1 Category</a>
<a href="#categories/2">Article 2 Category</a>
更新:我的其他REST调用正在运行,即使使用了诸如https://myrestcall.net/tables/articles/:articleID之类的路由参数,所以我专注于失败调用的?$ filter =方面。让其他人在将值传递到REST调用时遇到问题,因为变量不是直接位于/之后的/:articleID
答案 0 :(得分:1)
在pfcServices
工厂定义中将isArray:true
添加到'update': { method: 'PATCH' }
,每次获得JSON数组时,都需要将isArray: true
添加到方法定义中。
var pfcServices = angular.module('pfcServices', ['ngResource'])
pfcServices.factory('pfcArticleCategories', ['$resource', function ($resource) {
return $resource('https://myrestcall.net/tables/articles/?$filter=(articlecategoryid eq :articlecategoryID)', { articlecategoryID: '@articlecategoryid' },
{
'update': { method: 'PATCH', isArray:true }
}
);
}]);
isArray
- {boolean =} - 如果为true,则此操作的返回对象为数组,请参阅返回部分。
请参阅文档here
答案 1 :(得分:1)
解决了!问题出在控制器中,因为我正在使用“Get&#39;”。 A&#39; Get&#39;适用于通过id检索某些内容(例如https://myrestcall.net/tables/articles/:articleID)。
但是,我需要使用&#39;查询&#39;命令,因为它有一个隐含的isArray:true(请参阅Angular Resource Doc,其中说明&#39;查询&#39;:{方法:&#39; GET&#39;,isArray:true},)
当我从以下位置更改控制器时:
pfcControllers.controller('pfcCategoriesCtrl', ['$scope', '$routeParams', 'pfcArticleCategories', function ($scope, $routeParams, pfcArticleCategories) {
$scope.category = pfcArticleCategories.get({ articlecategoryID: $routeParams.articlecategoryID });
}]);
要:
pfcControllers.controller('pfcCategoriesCtrl', ['$scope', '$routeParams', 'pfcArticleCategories', function ($scope, $routeParams, pfcArticleCategories) {
$scope.category = pfcArticleCategories.query({ articlecategoryID: $routeParams.articlecategoryID });
}]);
根据需要解决。