2017-07-17 101 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); 
    } 
} 
相关问题