火库存储不允许我检索图像 URL

我已成功将文件上传到火库存储。


但是,当我包含状态已更改的 .on 函数时,控制台会引发以下错误:


错误:引用.push 失败:第一个参数在属性“见证.clientImg”中包含未定义的参数


我正在尝试重新获取图像URL并将其放在变量中。


我的代码如下所示,任何人都可以提供帮助吗?


  let itemsRef = firebase.database().ref('testimonials');

  let imgFile = this.state.currentImg;

  let imageRef;

  let imgURL;


  if (imgFile){

   imageRef = firebase.storage().ref("testimonialPics/"+imgFile.name).put(imgFile);



   imageRef.on('state_changed', (snapshot)=>{

    console.log(snapshot);

   })


  let clientObj = {

    name: this.state.currentName,

    feedback: this.state.currentComments,

    approved: false,

    clientImg: imgURL

  }


  itemsRef.push(clientObj);

  console.log(clientObj);


  this.setState({

    currentName: "",

    currentComments: "",

    currentImg: null

  })


呼啦一阵风
浏览 57回答 2
2回答

哆啦的时光机

您没有在此代码中的任何位置给出值,这与您调用时的事实相匹配(如错误消息所述,您无法将未定义的值写入数据库)imgURLundefineditemsRef.push(clientObj)看起来您正在尝试将下载 URL 写入数据库。为此,我将密切关注文档中的示例。但根据您当前的代码,它应该是这样的:  let itemsRef = firebase.database().ref('testimonials');  let imgFile = this.state.currentImg;  let imageRef;  let imgURL;  if (imgFile){   let ref = firebase.storage().ref("testimonialPics/"+imgFile.name);    let task = ref.put(imgFile);   task.then(() => {    // this runs when the upload has completed    ref.getDownloadURL().then(function(downloadURL) {      // this runs when the download URL has been determined      let clientObj = {        name: this.state.currentName,        feedback: this.state.currentComments,        approved: false,        clientImg: downloadURL      }      itemsRef.push(clientObj);      this.setState({        currentName: "",        currentComments: "",        currentImg: null      })    })  })

MYYA

https://firebase.google.com/docs/storage/web/download-files#download_data_via_url试试这个。我认为你听错了听众。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript