简体   繁体   English

Firebase getDownloadURL()不返回任何内容

[英]Firebase getDownloadURL() not returning anything

I'm try to upload in image to Firebase storage and then get the download URL from storage in order to save it to my database. 我尝试将图像上传到Firebase存储中,然后从存储中获取下载URL,以将其保存到我的数据库中。

The commented out line works so I know its a problem with the "getDownloadURL" function or either in my storage reference. 带注释的行有效,因此我知道它与“ getDownloadURL”功能有关或在我的存储参考中存在问题。 I have a similar function that works with a shorter reference (userId + '/' + profilePicture). 我有一个类似的功能,可用于较短的引用(userId +'/'+ profilePicture)。

let returnVal;

const image = 'data:image/jpeg;base64,' + result;

const pictures = firebase.storage().ref(userId + '/' + newPostKey +'/' + 'pictures');
pictures.putString(image, `data_url`);

//firebase.database().ref('Posts/' + userId + '/' + newPostKey).child('photoURL').set("ifni");

firebase.storage().ref(userId + '/' + newPostKey + '/' + pictures).getDownloadURL().then(function (url) {
  // Execute (unknown)
  returnVal = url;

  firebase.database().ref('Posts/' + userId + '/' + newPostKey).child('photoURL').set(returnVal);
})

You can get the url from the callback promise ans mentioned in this doc 您可以从此文档中提到的回调承诺中获取网址

pictures.putString(image, `data_url`).then(function(snapshot) {
  console.log('Uploaded a data_url string!');
  var url = snapshot.downloadURL;
  //add it to firestore
});

You can do something like below 您可以执行以下操作

storageRef = firebase.storage().ref(userId + '/' + newPostKey +'/' + 'pictures');
    parseUpload = storageRef.putString(image, 'data_url');
    parseUpload.on('state_changed', (_snapshot) => {
          },
            (_err) => {
              // handle error here
            },
            (success) => {
              let url = parseUpload.snapshot.downloadURL
            });
firebase.database().ref('Posts/' + userId + '/' + newPostKey).child('photoURL').set(url);

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

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