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

时间:2018-07-14 13:30:45

标签: javascript firebase firebase-realtime-database firebase-storage

我正在将图像上传到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 个答案:

答案 0 :(得分:1)

文件上传后,您没有将任何内容写入数据库。最简单的解决方法是在那里更新newAds

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