Filter AngularJS REST JSON Data: error:badcfg Response does not match configured parameter

Filter AngularJS REST JSON Data: error:badcfg Response does not match configured parameter

我正在尝试使用路由参数来过滤 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

我试图获取的值是文章类别 ID。如果我将 URL 放入浏览器工具(例如 Postman)中,它就可以工作。 articlecategoryid 想要的 returns 的例子 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>

以下是我如何 link 进入此页面以通过路由参数 (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

在你的 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=} – 如果为真,则此操作返回的对象是一个数组,请参阅 returns 部分。

查看文档 here

解决了!问题出在控制器中,因为我使用的是 'Get'。 'Get' 可以很好地通过 id 检索某些内容(例如 https://myrestcall.net/tables/articles/:articleID)。

但是,我需要使用 'query' 命令,因为它有一个隐含的 isArray:true(请参阅 Angular Resource Doc,它指出 'query':{method:'GET', 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 });
}]);

如愿以偿。