firebase.storage().ref().getDownloadURL() 不会运行
我正在尝试将图像上传到Firebase存储中,并将下载图保存在Firestore中,但是当我的代码运行时,.getDownloadurl()将无法运行。图像上传,但在那之后,什么都没有发生。我的代码在下面。我尝试了几张图像。
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)
})
})
})
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. 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)
})
})
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
console.log('Img ')
之后您没有收到任何结果或日志,因为您没有正确链接承诺。您必须在链接.then()
之前调用getDownloadURL()
。请参阅下面的代码:我对上面的代码留下了一些注释以便更好地理解。
如需了解更多信息,您可以查看此文档。
You're not getting any results or logs after
console.log('Img ')
because you're not chaining the promise properly. You must call thegetDownloadURL()
before chaining.then()
. See code below:I leave some comments on the code above for better understanding.
For more information, you may checkout this documentation.