2017-07-17 11 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

LOG2:私は画像のリンクを返すように得ることができない理由何らかの理由

を未定義?

答えて

1

thenpromiseを使用すると、あなたのタスクを非同期になり、後に実行されるイベントキューに置かれていますので、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); 
    } 
} 
前に実行されるため、
関連する問題