响应应包含一个对象,但得到的却是GET操作的数组
Expected response to contain an object but got an array for GET action
我收到这个错误"Error: [$resource:badcfg] Error in resource configuration for action 'get'. Expected response to contain an object but got an array
"
我不知道该怎么修。我有这项服务
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);
};
我知道我必须使用$resource-action isArray=true,但我不知道该把它放在哪里
angular.module('messages').factory('Messages', ['$resource',
function ($resource) {
return $resource('api/messages/:username', {
username: '@username'
},
{'query': {method: 'GET', isArray: true}});
}]);
但是没有结果并且仍然存在相同的误差。
在控制器中:
$scope.findOne = function () {
$scope.messages = Messages.query({
username: $routeParams.username
});
console.log($scope.messages);
};
query
而不是get
,应该解决它。
使用Messages.query(…)而不是方法get()
您所做的是正确的,但您使用了刚刚创建的方法('query'),所以调用看起来像这样:
$scope.findOne = function () {
Messages.query({
username: $routeParams.username
}).$promise.then(function (response) {
$scope.messages = response;
console.log($scope.messages);
});
};
相关文章:
- 响应应包含一个对象,但得到的却是GET操作的数组
- Yui Get 复选框返回数组而不是布尔值
- 我需要引用使用 .get() 创建的数组
- Javascript POST数据进入GET PHP数组
- 使用AngularJS$http.get在滚动时重复请求JSON数组
- $.get-json数组复制
- 什么's将数组传递给app.get()的第一个参数的替换项
- JavaScript $http.get 一个 json 数组并将其存储在 localstorage 中
- 尝试循环访问数组并为每个数组添加一个 get 函数
- PHP:如何在 PHP 中使用 GET 将值存储到数组中
- RxJS 合并的 GET 请求的 url 数组
- AngularJS - 将多个 get 请求收集到 json 数组中,然后传递给指令
- JavaScript 对象数组 Group By 和 Get 值
- 获取 get JSON 显示数组中的值时遇到问题
- 无法弄清楚 $.get 上的回调以将文本文件读取到数组中
- Angular2:从http.get响应中提取数组元素
- 将 jquery get 请求数组传递给 $.when()
- 如何:以ajax get数组作为参数的回调函数
- 离开后.get数组丢失数据
- javascript中函数外的get数组值的问题