TypeError:无法读取未定义的 firebase 和 vuex 的属性“0”
2018-11-05
459
我正在使用 vuex 动作,当我尝试获取已上传图像的 url 时发生这种情况,一切正常并且图像已保存在 firebase 中,但我无法获取 downloadUrl 并将其添加到我的 meetup 数据库的 downloadurl 引用中
我的代码如下所示
actions: {
createMeetup ({commit, getters}, payload) {
const meetup = {
title: payload.title,
location: payload.location,
description: payload.description,
date: payload.date.toISOString(),
creatorId: getters.user.id
}
let imageUrl
let key
firebase.database().ref('meetups').push(meetup)
.then((data) => {
key = data.key
return key
})
.then(key => {
const filename = payload.image.name
const ext = filename.slice(filename.lastIndexOf('.'))
return firebase.storage().ref('meetups/' + key + '.' + ext).put(payload.image)
})
.then(fileData => {
console.log(fileData)
imageUrl = fileData.metadata.downloadURLs[0]
return firebase.database().ref('meetups').child(key).update({imageUrl: imageUrl})
})
.then(() => {
commit('createMeetup', {
...meetup,
imageUrl: imageUrl,
id: key
})
})
.catch((error) => {
console.log(error)
})
}
1个回答
holla 我问我自己回答我得到了解决方案感谢大家没有回答
createMeetup ({commit, getters}, payload) {
const meetup = {
title: payload.title,
location: payload.location,
description: payload.description,
date: payload.date.toISOString(),
creatorId: getters.user.id
}
let imageUrl
let key
firebase.database().ref('meetups').push(meetup)
.then((data) => {
key = data.key
return key
})
.then(key => {
const filename = payload.image.name
const ext = filename.slice(filename.lastIndexOf('.'))
return firebase.storage().ref('meetups/' + key + '.' + ext).put(payload.image)
})
.then(snapshot => {
return new Promise((resolve, reject) => {
snapshot.ref.getDownloadURL().then(url => {
snapshot.downloadURL = url
resolve(snapshot)
})
})
})
.then((snapshot) => {
imageUrl = snapshot.downloadURL
return firebase.database().ref('meetups').child(key).update({imageUrl: imageUrl})
})
.then(() => {
commit('createMeetup', {
...meetup,
imageUrl: imageUrl,
id: key
})
})
.catch((error) => {
console.log(error)
})
},
EMATade
2018-11-05