【问题标题】:Object #<Resource> has no method 'push'对象 #<Resource> 没有方法 'push'
【发布时间】:2013-11-25 18:45:00
【问题描述】:

起初我有这个,但得到错误Expected response to contain an object but got an array:

angular.module('homeModule').factory("TodosFactory", ['$resource', function ($resource) {
return $resource('/api/todos/:todoId', {
    todoId: '@_id'
});
}]);

所以我改成这样:

angular.module('homeModule').factory("TodosFactory", ['$resource', function ($resource) {
return $resource('/api/todos/:todoId', {
    todoId: '@_id'
}, {'save': {method: 'POST', isArray:true}}); // this line is added
}]);

我正在将一个项目保存到数据库,并作为结果返回对象列表。返回一个 JSON 数组,但出现此错误。

有什么建议吗?

【问题讨论】:

    标签: node.js rest angularjs express angular-resource


    【解决方案1】:

    isArray 属性适用于请求和响应。见AngularJs $resource use of isArray for both request and response。

    由于请求不是数组,因此出现错误。您可以将其包装在一个数组中,然后将其拉出服务器端。

    【讨论】:

    • 好的,可能会解决问题,但是我已经写了代码,所以添加到数据库的项目将被返回,而不是所有元素,所以它会尊重 REST 架构.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多