过滤AngularJS REST JSON数据:错误:badcfg响应不匹配配置参数(Filter

2019-10-21 12:50发布

我试图用一个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。 如果我把我的网址到浏览器的工具,如邮差它的工作原理。 它返回articlecategoryid如所期望的示例URL如下:

https://myrestcall.net/tables/articles/?$filter=(articlecategoryid eq 1)

然而,在角用这个似乎没有解决,并产生错误。

下面是这个调用一些样本静态数据:

[
{
    "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调用,例如工作,即使有routeparam https://myrestcall.net/tables/articles/:articleID所以我专注于$过滤器=的失败呼叫的方面? 有其他人有问题与值传递为REST调用,如变量后不能直接/因为它是在/:条款ArticleID

Answer 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 - {布尔=} -如果为真则此操作返回的对象是一个数组,见返回部分。

见文档在这里



Answer 2:

解决它! 问题是在控制器我使用的是“GET”。 A“得到”工作正常,由ID检索的东西(例如, https://myrestcall.net/tables/articles/:articleID )。

不过,我需要使用“查询”命令,因为这有一个隐IsArray的:真(请参阅角资源文件 ,其中它指出“查询”:{方法:“GET”,IsArray的:真正})

当我从改变了我的控制器:

 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 });
}]);

它解析为所需。



文章来源: Filter AngularJS REST JSON Data: error:badcfg Response does not match configured parameter