首页 文章

Firebase存储:如何将图像URL存储为对象属性?

提问于
浏览
0

我正在将图像上传到firebase存储并检索其downloadURL . 我在firebase数据库中创建了一个ADS节点,在其节点中创建了一个newAd对象 . 我想要的是该图像的downloadURL保存在newAd.picLink中,即newAd(Object)的属性 .

addSubmitted.addEventListener("click", e => {
  const newAds = _db.ref("ADS").push();
  const newAd = {};

  const ref = firebase.storage().ref();
  const file = $("#exampleInputFile").get(0).files[0];
  const name = +new Date() + "-" + file.name;
  const task = ref.child(name).put(file, { contentType: file.type });

  task.snapshot.ref.getDownloadURL().then(downloadURL => {
    console.log("File available at", downloadURL);
    newAd.picLink = downloadURL; /*this isn't working how can i set 
    downloadURL as newAd objects property*/
  });
});

1 回答

  • 1

    上传文件后,您不会向数据库写入任何内容 . 最简单的解决方法是在那里更新 newAds

    task.snapshot.ref.getDownloadURL().then(downloadURL => {
      console.log("File available at", downloadURL);
      newAdS.update({ picLink: downloadURL });
    });
    

相关问题