【问题标题】:Firebase storage doesn't download and add to firebase databaseFirebase 存储不会下载并添加到 firebase 数据库
【发布时间】:2021-10-12 07:46:34
【问题描述】:

Firebase 存储不会下载并添加到 Firebase 数据库。 firebase.ref.getDownloadURL() 不起作用,imageUrl 不会添加到 firebase 数据库中。

代码如下:

createMeetup({
        commit,
        getters
    }, payload) {
        const meetup = {
            title: payload.title,
            location: payload.location,
            description: payload.description,
            date: payload.date,
            time: payload.time,
            creatorId: getters.user.id
        }
        3
        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.lastIndex0f('.'));
                return firebase.storage().ref('meetupsl' + key + '.' + ext).put(payload.image)
            })
            .then(fileData => {
                imageUrl = fileData.ref.getDownloadURL()
                return firebase.database().ref('meetups').chi1d(key).update({
                    imageUrl: imageUrl
                })
            })
            .then(() => {
                    commit('CREATE_MEETUP', {
                        ...meetup,
                        imageUrl: imageUrl,
                        id: key
                    }) 3
            })
    .catch((error) => {
            console.log(error);
            D;
        }:

【问题讨论】:

  • “firebase”:“^8.9.0”和“vue”:“^2.6.11”,
  • 请不要发布您的代码截图或其他文本内容。而是发布实际文本,并使用 Stack Overflow 的格式化工具对其进行标记。另外:不要在 cmets 中添加其他信息。相反,您可以单击问题下方的edit 链接在此处添加信息。

标签: javascript firebase vuejs2 vuex firebase-storage


【解决方案1】:

getDownloadURL() 方法返回一个 Promise,您需要等待。

比如:

...
.then(fileData => {
  return fileData.ref.getDownloadURL()
})
.then(imageUrl => {
  return firebase.database().ref('meetups').child(key).update({ imageUrl: imageUrl})
})
...

【讨论】:

    猜你喜欢
    • 2017-02-05
    • 2017-12-11
    • 1970-01-01
    • 2018-09-20
    • 2016-10-01
    • 2020-10-15
    • 2020-11-01
    • 1970-01-01
    • 2017-11-03
    相关资源
    最近更新 更多