[英]firebase.storage().ref().getDownloadURL() won't run
我正在尝试将图像上传到 Firebase 存储并将 downloadURL 保存在 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)
})
})
})
在console.log('Img ')
之后您没有得到任何结果或日志,因为您没有正确链接 promise。 您必须在链接getDownloadURL()
.then()
。 请参阅下面的代码:
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)
})
为了更好地理解,我对上面的代码留下了一些评论。 有关更多信息,您可以查看此文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.