简体   繁体   English

Firebase不使用TypeScript和Ionic3返回metaData中的downloadURL

[英]Firebase not returning downloadURL in metaData with typescript and Ionic3

I have been struggling for days now to get Firebase to return the downloadURL in the metadata. 我已经努力工作了好几天才能让Firebase返回元数据中的downloadURL。 By all accounts it should be there as the metadata includes fullPath and other info, but just not 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);
            });
        }
      });
  }

if I log that in the console, I can see everything except downloadURL 如果我在控制台中登录,则可以看到除downloadURL以外的所有内容

I also tried changing .set(savedPicture.downloadURL); 我也尝试更改.set(savedPicture.downloadURL); to .set(savedPicture.metadata.downloadURLS[0]); 设置为.set(savedPicture.metadata.downloadURLS [0]);

but still there are no downloadURL items in any of the response parameters. 但在任何响应参数中仍然没有downloadURL项。

Any ideas? 有任何想法吗?

after you upload a raw string using the putString method, use the same ref and use getDownloadUrl() method to get the url like below, 使用putString方法上传原始字符串后,请使用相同的ref并使用getDownloadUrl()方法获取如下所示的网址,

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

In your case, you can do like below 就您而言,您可以像下面这样

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

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM