$资源:预计响应包含一个对象,但得到的数组 [英] $resource : expected response to contain an object but got an array

查看:195
本文介绍了$资源:预计响应包含一个对象,但得到的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经如此存在于(如下:<一href=\"http://stackoverflow.com/questions/24409220/error-in-resource-configuration-expected-response-to-contain-an-object-but-got\">Error在资源配置。预期的响应包含一个对象,但有一个数组),即使我已经遇到这个错误前几天,我没有发现,为什么我得到现在。

I know this question already exists on SO (as here : Error in resource configuration. Expected response to contain an object but got an array ) and even if I've already faced this error a few days ago, I didn't find why I'm getting it now.

在一方面,我在一个RESTful API节点EX preSS 。这让我对数据库的访问,在那里我试图得到一些数据。当使用特定的 $资源的要求,我得到这个JSON(与邮差测试):

On the one hand, I have a RESTful API in Node Express. It gives me access to a database, where I'm trying to get some data. When using a specific $resourcerequest, I'm getting this JSON (tested with 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
   }
 ]

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

On the other hand, I have an Angular JS app. This one use a factory to get the data from the 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
                    }
        } 

我也有使用此工厂,试图执行与参数查询控制器:

I also have a controller which uses this factory, trying to execute the query with the param :

 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 来得到一些数据&LT; D​​IV NG-repeat'thing的东西'&GT; {{thing.ref}}&LT; / DIV&GT;

通过阅读其他职位,我确信添加 IsArray的:假工厂将解决这个问题,因为它已经在另一家工厂已经固定它。

By reading other posts, I was sure that adding an isArray : false to the factory would fix the problem, as it has already fixed it in another factory.

你知道吗?

编辑:感谢您​​的答复。我改变了我厂code有:

Edit : Thanks for your replies. I changed my factory code with :

.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 //
                        }
            } 

和它固定的错误。现在,我正在与我的 $范围内容的另一个问题,就是不确定的,并发送 $承诺:承诺,$解决:解决。我读 $资源文档来了解这意味着什么,但我仍然有不知道如何使它工作的想法。

And it fixed the error. Now, I'm working on another problem with my $scope content which is undefined and send $promise : Promise, $resolved : Resolved. I read $resource doc to understand what it means, but I still have no idea of how to make it work.

推荐答案

尝试:

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

这篇关于$资源:预计响应包含一个对象,但得到的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆