Firebase返回" Undefined" on" downloadURL"

时间:2018-06-11 07:04:15

标签: javascript reactjs firebase firebase-storage

我刚刚使用react和firebase(一个上传图片的简单应用程序,并在上传后,在页面上显示)进行测试。上传总是成功的,但我无法显示图像,因为" dowloadURL"未定义。

有人可以帮助我吗?

以下是React / Firebase呼叫代码



handleUpload(e) {
    console.log('handelUpload');
    const file = e.target.files[0];
    const storageRef = firebase.storage().ref(`/fotos/${file.name}`);
    const task = storageRef.put(file);

    task.on(
      'state_changed',
      snapshot => {
        let percentage = snapshot.bytesTransferred / snapshot.totalBytes * 100;
        this.setState({ uploadValue: percentage });
      },
      console.log,
      () => {
        const record = {
          photoURL: this.state.user.photoURL,
          displayName: this.state.user.displayName,
          image: task.snapshot.downloadURL
        };

        const dbRef = firebase.database().ref('pictures');
        const newPicture = dbRef.push();
        newPicture.set(record);
      }
    );
  }




这是我在上传时放置图片的代码



        <FileUpload onUpload={this.handleUpload} />

      {this.state.pictures.map(picture =>
        <div>
          <hr/>
          <img src={picture.image} alt=""/> 
          <br/>
          <img width="32" src={picture.photoURL} alt={picture.displayName}/> 
          <br/>
          <span>{picture.displayName}</span>
        </div>
      ).reverse()}                
    </div>
&#13;
&#13;
&#13;

谢谢!

1 个答案:

答案 0 :(得分:1)

在版本5中,downloadURL属性已被删除。请参阅:https://firebase.google.com/support/release-notes/js

您现在应该使用ref.getDownloadURL()(请注意,此方法会返回一个承诺)。