【问题标题】:Firebase Storage: How to store image url as an object property?Firebase 存储:如何将图像 url 存储为对象属性?
【发布时间】:2018-12-22 17:04:20
【问题描述】:

我正在将图像上传到 Firebase 存储并检索其下载 URL。我在 firebase 数据库中创建了一个 ADS 节点,并在其节点中创建了一个 newAd 对象。 我想要的是该图像的下载 URL 保存在 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*/
  });
});

【问题讨论】:

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


    【解决方案1】:

    文件上传后,您不会向数据库写入任何内容。最简单的修复方法是在其中更新newAds

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

    【讨论】:

      猜你喜欢
      • 2019-03-10
      • 2021-04-14
      • 2021-10-14
      • 2017-08-10
      • 2020-12-18
      • 2021-03-14
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多