【发布时间】:2018-12-30 00:12:27
【问题描述】:
我想在图像上传完成后获取下载 URL,然后将 downloadURL 设置为我的 newAds.picLink(object.property) ,但在此代码中,因为上传正在进行中
newAds.update({ picLink: downloadURL });
调用 会引发错误,因为 downloadURL 当前不可用且正在进行中。我已经通过使用 setTimeOut 到 8 秒解决了这个问题,这让图像首先完全上传,然后接收到 downloadURL,但这是不正确的。
一旦图像完全上传,我如何创建一个正确的回调来设置 newAds.picLink = downloadURL?
addSubmitted.addEventListener('click', e => {
const newAds = _db.ref(userID).push();
const newAd = {
};
const ref = firebase.storage().ref();
const file = $('#exampleInputFile').get(0).files[0];
const name = (+new Date() + '-' + file.name);
const task = ref.child(name).put(file, {contentType: file.type});
function abc() {
task.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log('File available at', downloadURL);
console.log(downloadURL);
newAds.update({ picLink: downloadURL });
});
task.catch(error => {
// Use to signal error if something goes wrong.
console.log(`Failed to upload file and get link - ${error}`);
});
}
setTimeout(abc,8000);
【问题讨论】:
-
你在哪里声明
newAds? -
@AlexIronside 代码更新,一旦点击提交按钮,这一切都会运行
标签: javascript firebase firebase-storage