firebase.storage().ref().getDownloadURL() 不会 运行
firebase.storage().ref().getDownloadURL() won't run
我正在尝试将图像上传到 Firebase 存储并将 downloadURL 保存在 Firestore 中,但是当我的代码为 运行ning 时,.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 ')
之后没有得到任何结果或日志,因为您没有正确链接承诺。您必须在链接 .then()
之前调用 getDownloadURL()
。请参阅下面的代码:
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)
})
为了更好地理解,我对上面的代码做了一些评论。
有关更多信息,您可以查看此 documentation.
我正在尝试将图像上传到 Firebase 存储并将 downloadURL 保存在 Firestore 中,但是当我的代码为 运行ning 时,.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 ')
之后没有得到任何结果或日志,因为您没有正确链接承诺。您必须在链接 .then()
之前调用 getDownloadURL()
。请参阅下面的代码:
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)
})
为了更好地理解,我对上面的代码做了一些评论。 有关更多信息,您可以查看此 documentation.