gpt4 book ai didi

javascript - 那么函数没有定义

转载 作者:行者123 更新时间:2023-11-29 14:43:35 24 4
gpt4 key购买 nike

我有一个函数来获取我需要存储在本地存储中的当前公司代码,并在其余的 API 调用中使用它。因此,在未检索到该公司代码的值之前,我不应该进行 API 调用。这就是我获取公司代码的方式

currentDivision = function() {
var q = $q.defer();
var division = window.localStorage.getItem("CurrentDivision");
if(division){
return q.resolve(division);
}
else{
var url = this.fooApi + "current/Me?$select=CurrentDivision";
$http.get(url)
.success(function(data) {
division = data.d.results[0].CurrentDivision;
window.localStorage.setItem("CurrentDivision", division);
return q.resolve(division);
})
}
return q.promise;
}

这就是我在确保成功检索当前分区后尝试调用其余 API 调用的方式:

$scope.openModal = function() {
$scope.modal.show().then(function() {
currentDivision().then(function(){
fooServices.getData().then(function() {
$scope.closeModal();
}, function(reason) {
// handle the error
$scope.showAlert(reason);
$scope.closeModal();
})
});
})
}

但是当我尝试这个时,我收到了这个错误信息:TypeError:无法读取未定义的属性“then”

最佳答案

你应该总是返回q.promise。删除 return 中的:

if (division) {
return q.resolve(division);
}

即:

var q = $q.defer();
// ...
if (division) {
q.resolve(division);
} else {
//...
}
return q.promise;

通过这种方式,您将返回已解决的 promise ,它会立即调用 then

关于javascript - 那么函数没有定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308803/

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