AngularJS - 错误:value.push不是函数

时间:2014-03-21 10:58:27

标签: angularjs post angularjs-resource

我有一个像这样定义的服务 - >

storegrServices.factory('productList', ['$resource', function($resource) {
    return $resource('', {},{
        query: {url:'/apis/productshome.php', method:'GET', isArray:false},
        getProductDetail: {url:'/apis/getproductdetail.php', method:'POST', isArray:true}
    });
}]);

在控制器中,这就是我调用POST的方式 - >

storegrControllers.controller('productPageCtrl', ['$scope','productList','$routeParams', function($scope, productList, $routeParams) {
    $scope.postVariable = new productList();
    $scope.postVariable.productcode =  $routeParams.code;
    $scope.postVariable.$getProductDetail();
}]);

这会将产品代码发送到服务器,服务器返回响应中的数组。我使用isArray:是的,但它仍然一直给我一个错误。我的代码中没有'value'变量/对象。

请建议我该如何解决这个问题。

这是我得到的完整错误 - >

Error: value.push is not a function
resourceFactory/</Resource[name]/promise</<@http://storegr.com/js-lib/angular-resource.js:530
q@http://storegr.com/js-lib/angular.min.js:7
resourceFactory/</Resource[name]/promise<@http://storegr.com/js-lib/angular-resource.js:529
yd/e/k.promise.then/w@http://storegr.com/js-lib/angular.min.js:92
yd/e/k.promise.then/w@http://storegr.com/js-lib/angular.min.js:92
yd/g/<.then/<@http://storegr.com/js-lib/angular.min.js:93
zd/this.$get</h.prototype.$eval@http://storegr.com/js-lib/angular.min.js:101
zd/this.$get</h.prototype.$digest@http://storegr.com/js-lib/angular.min.js:98
zd/this.$get</h.prototype.$apply@http://storegr.com/js-lib/angular.min.js:101
f@http://storegr.com/js-lib/angular.min.js:66
F@http://storegr.com/js-lib/angular.min.js:70
md/</B.onreadystatechange@http://storegr.com/js-lib/angular.min.js:71
http://storegr.com/js-lib/angular.min.js
Line 84

这是来自服务器的响应 - &gt;

[{"sno":21,"cat1":"Beverages","cat1code":"B","cat2":"Carbonated Drinks&Fruit Drinks","cat2code":"CDD","cat3":"Fruit Juices","cat3code":"FJ","cumulative":20,"brand":"Real Juice","product":"Apple","productcode":"B-CDD-FJ-1","imagename":"B-CDD-FJ-1","weight":200,"unit":0,"mrp":20,"margin":0,"wsp":0,"vat":0,"hbprice":0,"discount":0,"availableqty":0,"availableprice":0,"existingqty":0,"existingprice":0,"addedqty":0,"addedprice":0,"soldqty":0,"soldprice":0,"tags":"home"}]

1 个答案:

答案 0 :(得分:2)

我能够解决这个问题,在这里回答它,这样它也可以使其他人受益。

我将我的数组响应修改为像这样的json对象并且它有效。

{ "product":
[{"sno":21,"cat1":"Beverages","cat1code":"B","cat2":"Carbonated Drinks&Fruit Drinks","cat2code":"CDD","cat3":"Fruit Juices","cat3code":"FJ","cumulative":20,"brand":"Real Juice","product":"Apple","productcode":"B-CDD-FJ-1","imagename":"B-CDD-FJ-1","weight":200,"unit":0,"mrp":20,"margin":0,"wsp":0,"vat":0,"hbprice":0,"discount":0,"availableqty":0,"availableprice":0,"existingqty":0,"existingprice":0,"addedqty":0,"addedprice":0,"soldqty":0,"soldprice":0,"tags":"home"}]
}

谢谢。