gpt4 book ai didi

javascript - 返回 Promise 时出错,出现错误,因为 .then 不是 Angular JS 中的函数

转载 作者:行者123 更新时间:2023-12-01 03:05:02 26 4
gpt4 key购买 nike

我收到错误,因为 modifyProduct.then 不是函数,我读过一些文章,它说因为我没有返回任何 promise ,我如何可以实现这个,有人可以帮助我

在这里,我在 executionFromCompany 函数内调用 modifyProduct,然后在 Controller 内使用 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
})
});

最佳答案

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

改变这个

modifyProduct.then

到此

modifyProduct().then

关于javascript - 返回 Promise 时出错,出现错误,因为 .then 不是 Angular JS 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46253265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com