我正在尝试从Firebase存储中检索数据URL。我有一个获取网址的静态函数,但返回值始终是不确定的?如何获取文件的url并将其存储到数据库中?
uploadImageByDataURL(image, imageName, directory) {
const uploadTask = firebase.storage().ref(`images/${directory}/${imageName}`).putString(image, 'data_url');
uploadTask.on('state_changed', () => {
firebase.storage().ref(`images/${directory}`).child(`${imageName}`).getDownloadURL().then(url => {
return url
})
})
}
答案 0 :(得分:0)
检索下载URL异步进行。需要下载URL的任何代码都应位于相应的then()
块中。所以:
const uploadTask = firebase.storage().ref(`images/${directory}/${imageName}`).putString(image, 'data_url');
uploadTask.on('state_changed', () => {
firebase.storage().ref(`images/${directory}`).child(`${imageName}`).getDownloadURL().then(url => {
firebase.database().reference().set(url);
})
})