返回Promise时出错,将错误视为.then不是Angular JS中的函数

时间:2017-09-16 11:13:22

标签: javascript angularjs promise angular-promise

我收到的错误为 modifyProduct.then is not a function ,我读过一些文章并说它因为我没有回复任何承诺,我怎么能做到这一点,有人可以帮助我吗? / p>

我在modifyProduct函数中调用executionFromCompany,然后在控制器中使用executionFromCompany

 var app = angular.module('myApp', ["chart.js"]);
 app.factory('ProductsService', function($http) {
     function getProduct() {
         return $http.get('finalmsodetails.json').then(function(response) {
             //console.log(response.data);
             return response.data;
         });
     }

     function modifyProduct() {
         return getProduct().then(function(rawData) {
             newtest = rawData;
             //console.log('test', newtest.length);
             var lightData = rawData.map(function(item) {
                 // use Object.assign to prevent mutating original object
                 var newItem = Object.assign({}, item);
                 var lightExecutions = item.executions.map(function(d) {
                     var ld = {
                         id: d.id,
                         orderId: d.orderId,
                         executionStatus: d.executionStatus,
                         executedOn: d.executedOn,
                         executedBy: d.executedBy,
                         executedByDisplay: d.executedByDisplay,
                     };
                     return ld;
                 });
                 newItem.executions = lightExecutions;
                 return newItem;
             });
             return lightData;
         });
     }

     function executionFromCompany() {
         return modifyProduct.then(function(lightData) {
             executionByCompany = $filter('filter')(lightData.executions, function(inputs) {
                 if ((inputs.executedBy == 'a')) return inputs;
             });
             console.log(executionByCompany);
             return executionByCompany;
         });
     }
     return {
         getProduct: getProduct,
         modifyProduct: modifyProduct,
         executionFromCompany: executionFromCompany
     };
 });
 app.controller('MainCtrl', function($scope, ProductsService) {
     ProductsService.executionFromCompany().then(function(value) {
         console.log(value);
     }, function(err) {
         // Here will be if there was an error
     })
 });

1 个答案:

答案 0 :(得分:2)

modifyProduct是一个函数,而不是一个对象

更改此

modifyProduct.then

到这个

modifyProduct().then