[英]firebase.storage().ref().getDownloadURL() won't run
I am trying to upload an image to Firebase Storage and save the downloadURL in Firestore, but when my code is running, .getDownloadURL() won't run.我正在尝试将图像上传到 Firebase 存储并将 downloadURL 保存在 Firestore 中,但是当我的代码运行时,.getDownloadURL() 不会运行。 The image uploads, but after that, nothing happens.
图片上传,但之后没有任何反应。 My code is below.
我的代码如下。 I've tried with several images.
我试过几张图片。
const task = firebase.storage().ref('/hendingar/' + img.name).put(img)
task.then((snapshot) => {
console.log('Img ')
snapshot.ref.getDownloadURL((url) => {
console.log('Url')
firebase.firestore().collection('hendingar').add({
title: title,
description: description,
start: start,
end: end,
place: infoPlace,
prices: temp_arr,
img: url,
}).then(() => {
console.log('Success')
}).catch((error) => {
console.error(error)
})
})
})
You're not getting any results or logs after console.log('Img ')
because you're not chaining the promise properly.在
console.log('Img ')
之后您没有得到任何结果或日志,因为您没有正确链接 promise。 You must call the getDownloadURL()
before chaining .then()
.您必须在链接
getDownloadURL()
.then()
。 See code below:请参阅下面的代码:
const task = firebase.storage().ref('/hendingar/' + img.name).put(img)
// Uploads the file to Firebase Storage.
task.then((snapshot) => {
console.log('Img')
// You can also put the creation of Firestore Document here.
// (If you want it to create the document before getting the download URL.)
// Calls the `getDownloadURL()` method.
snapshot.ref.getDownloadURL()
.then((url) => {
// Returns the Download URL from Firebase Storage.
console.log('Url')
console.log(url)
// You can put this either here or before getting download URL.
firebase.firestore().collection('hendingar').add({
title: title,
description: description,
start: start,
end: end,
place: infoPlace,
prices: temp_arr,
img: url,
}).then(() => {
console.log('Success')
}).catch((error) => {
console.error(error)
})
})
})
.catch((error) => {
// Logs error if there's an error uploading of file.
console.log(error)
})
I leave some comments on the code above for better understanding.为了更好地理解,我对上面的代码留下了一些评论。 For more information, you may checkout this documentation .
有关更多信息,您可以查看此文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.