gpt4 book ai didi

javascript - Firebase存储,将图像检索到vue应用

转载 作者:行者123 更新时间:2023-12-03 06:44:24 25 4
gpt4 key购买 nike

我正在尝试从Firebase存储中下载图像以在Vue应用程序中进行渲染,从应用程序到Firebase存储的上传成功,但是在检索到它时出现了错误,我在Vue CLI中使用了Firebase SDK 3设置和vuex来管理我的状态。这是主store.js文件中我的操作中的功能设置

createMeetUp({commit, getters}, payload) {
//here my payload is an object contains the following props
const meetup = {
title: payload.title,
location: payload.location,
date: payload.date.toISOString(),
description: payload.description,
creatorId: getters.user.id
}

let imageUrl
let key

//now i am reaching out to the firebase database to store the above object
firebase.database().ref('meetup').push(meetup)
.then(data => {
key = data.key
return key
})
.then(key => {
//also in my payload object i stored an image file
//so here i am uploading the image to the firebase storage
const fileName = payload.image.name
const extension = fileName.slice(fileName.lastIndexOf('.'))
return firebase.storage().ref('meetup/' + key + '.' + extension).put(payload.image)
})
.then(imageInfo => {
//the issue is here in this then() block as i am stuck on how to retrieve the image from the storage to render it in the app
imageUrl = imageInfo.getDownloadURL()
return firebase.database().ref('meetups').child(key).update({
imageUrl: imageUrl
})
})
.then(() => {
//here i am simply commiting my mutation..
commit('createMeetUp', {
...meetup,
imageUrl: imageUrl,
id : key
})
})
.catch(err => console.log(err))







}


我得到的错误是:

TypeError: imageInfo.getDownloadURL is not a function



我再次相信问题出在 then()块中,我从Firebase存储中检索图像。
提前致谢

最佳答案

按照上面的评论,如果我没有误会(未测试...),以下各项应该可以工作。

注意,getDownloadURL()返回一个promise(请参阅here),因此您必须链接promise。

  ....
.then(key => {
//also in my payload object i stored an image file
//so here i am uploading the image to the firebase storage
const fileName = payload.image.name
const extension = fileName.slice(fileName.lastIndexOf('.'))
return firebase.storage().ref('meetup/' + key + '.' + extension).put(payload.image)
})
.then(uploadTaskSnapshot => {
return uploadTaskSnapshot.ref.getDownloadURL()
})
.then(imageUrl => {
return firebase.database().ref('meetups').child(key).update({
imageUrl: imageUrl
})
})
....

关于javascript - Firebase存储,将图像检索到vue应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52242991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com