2015-04-01 35 views
2

我得到这个错误“Error: [$resource:badcfg] Error in resource configuration for action 'get'. Expected response to contain an object but got an array期望的回应包含一个对象,但得到了GET行动

一个数组,我不知道如何解决它。我有这样的服务

angular.module('messages').factory('Messages', ['$resource', 
    function ($resource) { 
     return $resource('api/messages/:username', { 
      username: '@username' 
     }); 
    }]); 

,这在控制器:

$scope.findOne = function() { 
     $scope.messages = Messages.get({ 
      username: $routeParams.username 
     }); 

     console.log($scope.messages); 
    }; 

这条路我在API控制器此

exports.read = function (req, res) { 
    res.json(req.message); 
}; 

我知道,我必须使用$资源行动IsArray的= true,但我不知道该把它放在哪里。我试图做这样的事情:

angular.module('messages').factory('Messages', ['$resource', 
    function ($resource) { 
     return $resource('api/messages/:username', { 
      username: '@username' 
     }, 
      {'query': {method: 'GET', isArray: true}}); 
    }]); 

但没有结果,仍然是同样的错误。

+1

化妆IsArray的假 – Anita 2015-04-01 13:56:20

+0

没错@Anita'{ '查询':{方法: 'GET',IsArray的:假}});'作为响应是一个对象不是一个数组。 – 2015-04-01 13:57:28

回答

4

在你的控制器:

$scope.findOne = function() { 
     $scope.messages = Messages.query({ 
      username: $routeParams.username 
     }); 

     console.log($scope.messages); 
    }; 

query相反的get,应该解决这个问题。

3

使用Messages.query(...)而不是get()方法

1

你的所作所为是正确的,但你必须使用你刚刚创建的方法(“查询”),因此呼叫看起来像这样:

$scope.findOne = function() { 
    Messages.query({ 
     username: $routeParams.username 
    }).$promise.then(function (response) { 
     $scope.messages = response; 
     console.log($scope.messages); 
    }); 

}; 
相关问题