2016-11-23 180 views
1

我的問題是,角犯規等待的承諾得到解決。 你可以從控制檯檢查看到,它打印,它沒有得到供應商和技能對象並低於承諾返回。角不等待承諾解決

添加下面是我認爲最相關的部分代碼。我使用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}] 

謝謝所有幫助:)

+0

即承諾是如何工作的:角不會等待一個承諾來解決,你將不得不把需要一直是一個'。那麼()'回調內部解決的承諾代碼。製作方法,如'vm.getProviderById()'返回他們所使用的承諾,然後移動訪問'scope.provider' $'成。然後()'的代碼。 – Duncan

+0

如果你看一下getProviderById的函數定義的實現調用這使得HTTP請求,並返回'「返回$ Q的providerService。當(供應商);「'這是在成功。然後調用該服務後處理,$範圍服務提供商設置有成爲響應,所以我不明白您的回答,請詳細說明 –

回答

2

的問題是,當你調用函數vm.getProviderByIdvm.getProviderSkills它們調用的返回一個承諾等功能,但是這些功能本身並沒有。因此他們被調用,然後繼續下一步。

如果這些功能確實相互依賴,則需要將承諾鏈接在一起,或者從各自承諾的success中調用它們。

+0

它將幫助,如果你能寫一段代碼解釋,因爲我定義vm.getProviderById撥打服務功能,使HTTP請求和呼叫後有一個處理的響應上取得成功的。然後 –

0

在提供控制器的使用情況:

$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()的回報,你可以肯定的唯一的事情就是沒有.then()代碼的vm.getProviderId()尚未調用。但是,然後你嘗試訪問$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()你,你中止序列的休息,你在最後來處理在一個地方/日誌錯誤。