Javascript Firebase存储不允许我检索图像URL

Javascript Firebase存储不允许我检索图像URL,javascript,firebase,firebase-realtime-database,firebase-storage,Javascript,Firebase,Firebase Realtime Database,Firebase Storage,我正在成功地将文件上载到firebase存储 但是,当我使用statechanged包含.on函数时,控制台会抛出以下错误: 错误:Reference.push失败:第一个参数在属性'Certificationals.clientImg'中包含未定义的内容 我试图检索图像URL并将其放入变量中 我的代码如下所示,有人能帮忙吗 let itemsRef = firebase.database().ref('testimonials'); let imgFile = this.state.c

我正在成功地将文件上载到firebase存储

但是,当我使用statechanged包含.on函数时,控制台会抛出以下错误:

错误:Reference.push失败:第一个参数在属性'Certificationals.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
  })
试试这个。我想你听错人了。


试试这个。我认为你听到了错误的侦听器。

你没有在这个代码中的任何地方给
imgURL
一个值,这与调用
itemsRef.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
      })
    })
  })

在这段代码中,您没有在任何地方给
imgURL
一个值,这与调用
itemsRef.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
      })
    })
  })

没有代码。您共享了调用
push()
,因此错误不太可能来自此代码。请仔细检查错误消息和堆栈跟踪,将它们包括在您的问题中,并确保我们有引发错误的代码。Hi@FrankvanPuffelen我没有包括可能触发错误的推送方法。您没有给出值anywh在此代码中,这与调用
itemsRef.push(clientObj)
时它是
undefined
和(正如错误消息所说,您不能将未定义的值写入数据库。没有任何代码。您共享了调用
push()
,因此错误不太可能来自此代码。请仔细检查错误消息和堆栈跟踪,将它们包括在您的问题中,并确保我们有引发错误的代码。Hi@FrankvanPuffelen我没有包括可能触发错误的推送方法。您没有给出值anywh在此代码中,这与调用
itemsRef.push(clientObj)
时它是
undefined
的事实相匹配(正如错误消息所说,您不能将未定义的值写入数据库)。