我的問題是,角犯規等待的承諾得到解決。 你可以從控制檯檢查看到,它打印,它沒有得到供應商和技能對象並低於承諾返回。角不等待承諾解決
添加下面是我認爲最相關的部分代碼。我使用angulars庫 - $ q做了這個服務,並使用了延遲,然後和選項 - 如下面的服務中所見。
提供服務:
this.getProviderById = function (providersId) {
// will hold backend provider of the provider with id = providersId
var provider = undefined;
// if provider object is not defined then start the new process to fetch it
if (!provider) {
// create deferred object using $q
var deferred = $q.defer();
// get provider form backend
$http.get('http://localhost:3000/providers/getbyid/' + providersId)
.then(function (result) {
console.log('provider by id result - ' + JSON.stringify(result.data));
// save fetched provider to the local variable
provider = result.data;
// resolve the deferred
deferred.resolve(provider);
}, function (error) {
providers = error;
deferred.reject(error);
});
// set the provider object to be a promise until result comeback
provider = deferred.promise;
}
return $q.when(provider);
};
提供商控制器定義:
var vm = this;
vm.getCurrentUser = getCurrentUser;
function getCurrentUser(){
console.log('returning current user from auth service');
return authService.getCurrentUser();
}
vm.getProviderById = getProviderById;
function getProviderById(providersId){
providerService.getProviderById(providersId)
.then(function onSuccess(providerResult){
console.log('providerResult - ' + JSON.stringify(providerResult));
$scope.provider = providerResult;
}, function onError(error){
console.log('error getting provider by id - ' + error);
});
}
vm.getProviderSkills = getProviderSkills;
function getProviderSkills(providersId){
providerService.getProvidersSkills(providersId).then(function onSuccess(resultSkills){
console.log('resultSkills - ' + JSON.stringify(resultSkills));
$scope.skills = resultSkills;
}, function onError(error){
console.log('error getting providers - ' + $scope.provider + ' - skills');
});
}
提供控制器的使用情況:
$scope.user = vm.getCurrentUser();
if ($scope.user !== null && $scope.user !== undefined) {
console.log('user - ' + JSON.stringify($scope.user));
} else {
vm.initUsersProvider();
console.log('initiated users provider');
}
vm.getProviderById($scope.user.provider);
if ($scope.provider !== null && $scope.provider !== undefined) {
console.log('provider - ' + JSON.stringify($scope.provider));
} else {
console.log('didnt get provider object');
}
vm.getProviderSkills($scope.provider);
if ($scope.skills !== null && $scope.skills !== undefined) {
console.log('provider - ' + JSON.stringify($scope.skills));
} else {
console.log('didnt get skills object');
}
檢查上面的圖片的控制檯顯示結果:
user - {"_id":"5835b06b975ace23244bf205","email":"[email protected]","password":"","fbuser":"5835b06a975ace23244bf204","provider":"5835b06f975ace23244bf206","admin":false,"__v":0}
ProviderRegisterController.js:14 providersId - 5835b06f975ace23244bf206
providerService.js:74 providersId - 5835b06f975ace23244bf206
ProviderRegisterController.js:110 didnt get provider object
ProviderRegisterController.js:117 didnt get skills object
ProviderRegisterController.js:122 no availability module found
ProviderRegisterController.js:124 no bio module found
providerService.js:83 provider by id result - {"_id":"5835b06f975ace23244bf206","bio":"","__v":2,"references":[],"availability":{"auto":true},"skills":["5835b220975ace23244bf208","5835b2ff975ace23244bf209"]}
ProviderRegisterController.js:17 providerResult - {"_id":"5835b06f975ace23244bf206","bio":"","__v":2,"references":[],"availability":{"auto":true},"skills":["5835b220975ace23244bf208","5835b2ff975ace23244bf209"]}
providerService.js:56 received skills from db - [object Object],[object Object]
ProviderRegisterController.js:27 resultSkills - [{"_id":"5835b220975ace23244bf208","topic":2,"description":"sdfsdf","price":45,"status":"PENDING","__v":0},{"_id":"5835b2ff975ace23244bf209","topic":1,"description":"ddfgdfg","price":45,"status":"PENDING","__v":0}]
謝謝所有幫助:)
即承諾是如何工作的:角不會等待一個承諾來解決,你將不得不把需要一直是一個'。那麼()'回調內部解決的承諾代碼。製作方法,如'vm.getProviderById()'返回他們所使用的承諾,然後移動訪問'scope.provider' $'成。然後()'的代碼。 – Duncan
如果你看一下getProviderById的函數定義的實現調用這使得HTTP請求,並返回'「返回$ Q的providerService。當(供應商);「'這是在成功。然後調用該服務後處理,$範圍服務提供商設置有成爲響應,所以我不明白您的回答,請詳細說明 –