【问题标题】:Firebase not returning downloadURL in metaData with typescript and Ionic3Firebase 未使用 typescript 和 Ionic3 在元数据中返回 downloadURL
【发布时间】:2018-11-18 08:56:06
【问题描述】:

我几天来一直在努力让 Firebase 在元数据中返回 downloadURL。所有人都认为它应该在那里,因为元数据包括 fullPath 和其他信息,但不包括 downloadURL。

  addItem(
itemName: string,
eventId: string,
itemPicture: string = null
): PromiseLike<any> {
    return this.activityListRef
      .child(`${eventId}/itemList`)
      .push({ itemName })
      .then(newItem => {
        this.activityListRef.child(eventId).transaction(event => {
          return event;
        });
        if (itemPicture != null) {
          return firebase
            .storage()
            .ref(`/item/${newItem.key}/profilePicture.png`)
            .putString(itemPicture, 'base64', { contentType: 'image/png' })
            .then(savedPicture => {
            console.log(savedPicture.metadata);
              this.activityListRef
                .child(`${eventId}/itemList/${newItem.key}/profilePicture`)
                .set(savedPicture.downloadURL);
            });
        }
      });
  }

如果我在控制台中记录,我可以看到除了 downloadURL 之外的所有内容

我也尝试过改变 .set(savedPicture.downloadURL); 到 .set(savedPicture.metadata.downloadURLS[0]);

但在任何响应参数中仍然没有 downloadURL 项。

有什么想法吗?

【问题讨论】:

  • 在最新的 Cloud Storage for Firebase SDK 中,下载网址不再位于元数据中。相反,您需要在存储参考上调用 getDownloadUrl() 以获取它,如 MuruGan 的答案所示。
  • 谢谢弗兰克,这绝对是问题所在。我现在就拍。

标签: javascript firebase ionic-framework ionic3 firebase-storage


【解决方案1】:

使用 putString 方法上传原始字符串后,使用相同的 ref 并使用 getDownloadUrl() 方法获取 url,如下所示,

storage()
        .ref(`/item/${newItem.key}/profilePicture.png`).getDownloadUrl() // which returns promise in turn returns the image url once it's available.

在你的情况下,你可以这样做

var storageRef = firebase.storage().ref(`/item/${newItem.key}/profilePicture.png`)

return storageRef.putString(itemPicture, 'base64', { contentType: 'image/png' })
        .then(savedPicture => {
              storageRef.getDownloadUrl()
                .then(url =>{
                  this.activityListRef
                 .child(`${eventId}/itemList/${newItem.key}/profilePicture`)
                 .set(url);
         });             
 });

希望对你有帮助

【讨论】:

  • 谢谢MuruGan,就是这样。
  • 不提。很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 2018-05-26
  • 2020-04-28
  • 2018-11-07
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 2016-04-04
相关资源
最近更新 更多