2017-08-19 78 views
-1

在這條線就顯示無法訪問的代碼編譯時錯誤:無法訪問的代碼

resolve(data) 

是什麼原因造成的?

我的代碼:

zipCodeBlurEventCall(zipcode) { 
    if (!this.validationService.isValidZipCodeRequired(this.siteDetail.ZipCode)) { 
     this.clearInvalidFlags(); 
     this.invalidZipCode = true; 
     this.siteDetail.WeatherLocation = ""; 
     this.toastr.error("Please enter valid Zip Code"); 
    } 
    else {    
     this.siteLoading = true; 
     this.queryString = "?zipcode=" + zipcode; 

     return new Promise((resolve, reject) => { 
      this.siteService.getStationByZipCode(this.queryString).then(data => { 
       this.element = data; 

       if (this.element == null) { 
        this.siteLoading = false; 
        this.invalidZipCode = true; 
        this.toastr.error("Invalid Zip Code"); 
        return true; 

       } else { 
        this.siteDetail.StateName = this.states.find(item => item.StateId === this.siteDetail.StateId).StateName; 
        if (this.siteDetail.StateName.toLowerCase() == this.element.response.place.stateFull.toLowerCase()) { 

         this.siteDetail.WeatherLocation = this.element.response.place.name; 
         this.isWeatherFlag = true; 
         this.siteLoading = false; 
         this.invalidZipCode = false; 
         return false; 
        } 

        else { 
         this.siteLoading = false; 
         this.invalidZipCode = true; 
         this.toastr.error("Zip code does not match with the selected State"); 
         return true; 
        } 
       } 

       resolve(data);     
      }); 

    });    

    } 
} 
+0

我剛醒來,但它看起來好像總是在可以達到「resolve」前返回,這意味着它永遠不會運行。儘管如此,你的大括號還是很不自在,所以很難通過查看它們的範圍來判斷它們。 – Carcigenicate

回答

0

我不知道你爲什麼在這裏返回「真」或「假」值,但如果你仍然想你的代碼工作,你可以做如下,其中您在調用解析之後從函數返回:

zipCodeBlurEventCall(zipcode) { 
    if (!this.validationService.isValidZipCodeRequired(this.siteDetail.ZipCode)) { 
     this.clearInvalidFlags(); 
     this.invalidZipCode = true; 
     this.siteDetail.WeatherLocation = ""; 
     this.toastr.error("Please enter valid Zip Code"); 
    } 
    else {    
     this.siteLoading = true; 
     this.queryString = "?zipcode=" + zipcode; 

     return new Promise((resolve, reject) => { 
      this.siteService.getStationByZipCode(this.queryString).then(data => { 
       this.element = data; 
       let returnVal; 
       if (this.element == null) { 
        this.siteLoading = false; 
        this.invalidZipCode = true; 
        this.toastr.error("Invalid Zip Code"); 
        returnVal = true; 

       } else { 
        this.siteDetail.StateName = this.states.find(item => item.StateId === this.siteDetail.StateId).StateName; 
        if (this.siteDetail.StateName.toLowerCase() == this.element.response.place.stateFull.toLowerCase()) { 

         this.siteDetail.WeatherLocation = this.element.response.place.name; 
         this.isWeatherFlag = true; 
         this.siteLoading = false; 
         this.invalidZipCode = false; 
         returnVal = false; 
        } 

        else { 
         this.siteLoading = false; 
         this.invalidZipCode = true; 
         this.toastr.error("Zip code does not match with the selected State"); 
         returnVal = true; 
        } 
       } 

       resolve(data); 
       return returnVal;    
      }); 

    });    

    } 
}