2017-07-17 98 views
0

我似乎無法獲得基於文件名的存儲器中的圖像的下載URL。Ionic 2 Firebase從存儲器中獲取圖像URL

糾正,我可以,但我似乎無法得到變量功能。例如。我的代碼:

public getVenueImage(image: string){ 
    let imgUrl: string; 
    try{ 
     this.firebase.storage().ref().child("/venues/" + image).getDownloadURL().then(function(url){ 
     imgUrl = url; 
     console.log("log1: " + url); 
     }); 
    } 
    catch(e){ 
     console.log(e); 
    } 
    console.log("log2: " + imgUrl); 
    return imgUrl; 
    } 

LOG1:https://firebasestorage.googleapis.com/v0/b/icorp-dashboard.appspot.com/o/venues%2Fcinema.jpg?alt=media&token=e5be11ef-f53f-48dc-ab79-a81a50c0e317

的log 2:未定義

任何原因,我不能讓圖像鏈接返回?

回答

1

,因爲使用promisethen讓你的任務異步的,它被放置在事件隊列稍後執行,所以執行console.log("log2: " + imgUrl);imgUrl = url;

public getVenueImage(image: string){ 
    let imgUrl: string; 
    try{ 
     this.firebase.storage().ref().child("/venues/" + image).getDownloadURL().then(function(url){ 
     console.log("log1: " + url); 
     return url; 
     }); 
    } 
    catch(e){ 
     console.log(e); 
    } 
} 
相關問題