我的问题是,角度不等待应许得到解决。正如您从控制台的检查中看到的那样,它打印出它没有得到提供商和技能对象,并且在承诺返回的下方。
我认为,下面是最相关的代码部分。我使用Angulars库进行了服务-U 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":"iairbluer@gmail.com","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
和vm.getProviderSkills
时,他们调用其他函数,这些功能返回承诺,但是这些函数本身不会。因此,他们被调用,然后继续进行下一步。
如果这些功能实际上确实彼此依赖,则需要将承诺链在一起或从各自承诺的success
中拨打它们。
在提供商控制器的用法中:
$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');
}
当调用vm.getProviderId()
返回时,您唯一可以确定的是vm.getProviderId()
中的.then()
代码都没有被调用。但是,然后您尝试访问$scope.provider
,并且不会设置它。
getProviderId
必须返回承诺,以指示何时完成。如果需要,您也可以使用provider
解决承诺(这可能比在此处分配范围更好)。
vm.getProviderById = getProviderById;
function getProviderById(providersId){
return providerService.getProviderById(providersId)
.then(function onSuccess(providerResult){
console.log('providerResult - ' + JSON.stringify(providerResult));
return provider;
}, function onError(error){
console.log('error getting provider by id - ' + error);
});
}
和以类似方式更改getProviderSkills()
也返回最终的承诺。
现在您像这样使用它:
vm.getProviderById($scope.user.provider)
.then(function(provider) {
$scope.provider = provider; // If you still need to save it in scope.
if (provider !== null && provider !== undefined) {
console.log('provider - ' + JSON.stringify(provider));
} else {
return $q.reject('didnt get provider object');
}
return vm.getProviderSkills(provider);
})
.then(function(skills) {
if (skills === null || skills === undefined) {
return $q.reject('didn't get skills object');
}
$scope.skills = skills;
},
function failureCallback(error) {
console.log(error);
});
请注意,通过返回$ q.reject()您您中止其余的序列,并且在末端有一个位置来处理/记录错误。