【发布时间】:2021-02-02 07:14:03
【问题描述】:
try {
(async ()=> await Promise.all(this.state.selectedFile.map(picture =>
new Promise((resolve, reject) => {
Firebase.getStorageRef(`/${picture.name}`)
.put(picture)
.on('state_changed', (snapshot) => {
// progress function ....
const progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
console.log("Progress: ", progress)
},
reject,
() => {
// complete function ....
Firebase.getStorageRef(`/${picture}`)
.getDownloadURL()
.then(url => {
this.setState({mage:this.state.mage.concat([url])})
console.log(this.state.mage,"yooo")
});
});
})
)).then( Firebase.getDatabaseRef().push({
image:[this.state.mage],
thumbImage:[this.state.mage]
}).then(()=>{
console.log("added")
})))();
} catch (error) {
console.error(error);
}
在上面的代码中,使用 Promise 上传了一个图像列表。上传每张图片时的所有方法都使用 .then() 方法将 url 存储在一个列表中,以将其传递给另一个 .then() 并将其存储在数据库中。
错误:
图片的url为空,因为它在上传完成之前被执行。
上传完成后不是.then()方法负责执行吗?
【问题讨论】:
标签: reactjs asynchronous firebase-realtime-database async-await firebase-storage