【问题标题】:$resource : expected response to contain an object but got an array$resource : 预期响应包含一个对象但得到一个数组
【发布时间】:2015-08-20 01:56:28
【问题描述】:

我知道这个问题已经存在于 SO 上(如这里:Error in resource configuration. Expected response to contain an object but got an array),即使我几天前已经遇到过这个错误,我也没有找到为什么我现在得到它。

一方面,我在Node Express 中有一个 RESTful API。它使我可以访问数据库,我试图在其中获取一些数据。当使用特定的$resourcerequest 时,我得到了这个 JSON(用 Postman 测试):

 [
   {
    "ref": 2,
    "type": "string here",
    "matriculeMD": 4567,
    "name": "William",
    "date": "2016-09-14T22:00:00.000Z",
    "client": 1
   },
   {
    "ref": 4,
    "type": "string here",
    "matriculeMD": 1589,
    "name": "Averell",
    "date": "0000-00-00",
    "client": 1
   }
 ]

另一方面,我有一个 Angular JS 应用程序。这个使用工厂从 API 获取数据:

 .factory('Things', ['$resource', function($resource) {

    return $resource(

        'api_url/:client_id',
        {client_id: '@client_id'}, // getting some data from an ID
        {'query' : {
                        method : 'GET', // ofc, it's a get request in the API
                        isArray : false // to be sure to get an object and not an array
                    }
        } 

我还有一个使用这个工厂的控制器,尝试使用参数执行查询:

 app.controller('ctrl', ['$scope','Things', function ($scope,$things) 

    {
      $scope.thing = $things.query({},{'client_id':'1'}); 
      // replacing '@client_id' from factory with 1. I also tried with quoteless 1.
    }
 ]);

最后,我有一个 html 视图,我尝试使用我应该在 $scope.thing 中获取的数据和一些 <div ng-repeat'thing in things'>{{thing.ref}}</div>

通过阅读其他帖子,我确信将isArray : false 添加到工厂可以解决问题,因为它已经在另一家工厂修复了它。

有什么想法吗?

编辑:感谢您的回复。我改变了我的工厂代码:

.factory('Things', ['$resource', function($resource) {

        return $resource(

            'api_url/:client_id',
            {client_id: '@client_id'}, // getting some data from an ID
            {'query' : {
                            method : 'GET', // ofc, it's a get request in the API
                            isArray : true //
                        }
            } 

它修复了错误。现在,我正在处理我的$scope 内容未定义的另一个问题并发送$promise : Promise, $resolved : Resolved。我阅读了$resource 文档以了解它的含义,但我仍然不知道如何使它工作。

【问题讨论】:

  • 那么您的 API 到底返回了一个数组? isArray 不会转换数据,它只是告诉 Angular 期望的数据结构。

标签: javascript json angularjs


【解决方案1】:

正如 doldt 之前所说,您实际上期望的是一个数组。

您的响应是一个对象数组。

要么将 isArray 更改为 true,要么将服务器上的响应更改为返回一个对象。

一旦你改变了这一点,你就可以通过这种方式在控制器上使用你的资源(为 $resource 提供回调,因为你现在正在处理 Promise):

$things.query( {}, { 'client_id' : '1' }, function( response ){
    $scope.thing = response;
}); 

【讨论】:

  • 那么在这种情况下我该怎么办?改变我在 html 文件中使用数据的方式?
  • @Arhyaa 将您的服务称为$scope.things = $things.query({},{'client_id':'1'}); ,然后您可以像编写<div ng-repeat'thing in things'>{{thing.ref}}</div> 一样在html 模板中进行迭代。当然将isArray 设置为true。
  • 只需将 isArray 更改为 true :)
  • 我改了。没有更多错误,但看起来$scope.thing 是空的。我使用console.log 显示范围,我得到$promise : Promise, $resolved : false。是不是表示查询不成功?
  • 更新了我的答案。希望对你有帮助
【解决方案2】:

我认为您在使用 isArray 标志时做错了。如果您要发送数组,它必须是 true

试试看……

【讨论】:

  • 谢谢,cmets 里已经说过了。代码中没有更多错误,但我目前无法使用我得到的东西。当我显示$scope.thing 时,我得到$promise : Promise, $resolved : Resolved,而我的$scope 内容未定义。我要编辑我的第一篇文章:)
  • 试试这个 $things.query({},{'client_id':'1'}).$promise.then(function(data){ $scope.thing = data; });
  • 我之前做过类似的事情,以便在设置承诺后立即使用范围数据,但它没有改变任何东西,作为您的解决方案。我可能在我的应用程序结构或类似的东西中遗漏了其他东西吗?
【解决方案3】:

// Code goes here

var myApp = angular.module('myApp', ['ngResource']);

myApp.factory('Things', ['$resource',
  function($resource) {

    return $resource(

      'data/sample.json'
      //, {
      //  client_id: '@client_id'
      //}, // getting some data from an ID
      //{
      //  'query': {
      //    method: 'GET', // ofc, it's a get request in the API
      //    isArray: true // to be sure to get an object and not an array
      //  }
      //})
    )
  }
]);

myApp.controller('myCtrl', function($scope, Things) {

  Things.query({}, {
    'client_id': '1'
  }).$promise.then(function(data) {
    $scope.thing = data;
    console.log($scope.thing);
  })
});
[
   {
    "ref": 2,
    "type": "string here",
    "matriculeMD": 4567,
    "name": "William",
    "date": "2016-09-14T22:00:00.000Z",
    "client": 1
   },
   {
    "ref": 4,
    "type": "string here",
    "matriculeMD": 1589,
    "name": "Averell",
    "date": "0000-00-00",
    "client": 1
   }
 ]
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <script data-require="angular.js@*" data-semver="2.0.0-alpha.26" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-resource.min.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-controller="myCtrl">
  <h1>{{1/2}}</h1>
  <ul ng-repeat="t in thing">
    <li>{{t.name}}</li>
  </ul>
</body>

</html>

--输出 0.5

威廉 阿弗雷尔

【讨论】:

    【解决方案4】:

    试试:

    $things.query({},{'client_id':'1'}).$promise.then(function(result){
        $scope.thing = result;
        console.log($scope.thing);
    });
    

    【讨论】:

    • PAWAN RAJ 告诉我同样的事情,但它没有改变任何东西:/
    猜你喜欢
    • 1970-01-01
    • 2014-05-14
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    相关资源
    最近更新 更多