首页 > 解决方案 > Firebase 没有使用 typescript 和 Ionic3 在元数据中返回 downloadURL

问题描述

几天来,我一直在努力让 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 项。

有任何想法吗?

标签: javascriptfirebaseionic-frameworkionic3firebase-storage

解决方案


使用 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);
         });             
 });

希望这可以帮助


推荐阅读