如何在索引0的`put`中修复无效参数:使用React,Firebase和Filepond时预期的Blob或File

时间:2019-06-02 16:19:45

标签: reactjs firebase firebase-storage filepond

在创建Firebase用户时,我正在尝试上传用户头像,在此处创建用户后

auth.createUserWithEmailAndPassword(this.state.email,this.state.password)
    .then(credential=>{
     const photoUrl = this.handleUpload(credential)
})

我创建了一个句柄上传函数,该函数在下面返回一个photoUrl

handleUpload=(credential)=>{
    const {avatar} = this.state;
    const upload = storage.ref(`avatars/${credential.user.uid}`).put(avatar)
    upload.on('state_changed',(snapshot)=>{
      let progress = (snapshot.bytesTransferred/snapshot.totalBytes)*100;
      console.log('Upload is ' + progress + '% done');
    },(e)=>{
      console.log(e)
    },()=>{
      return storage.ref("avatars").child(`${credential.user.uid}`).getDownloadURL()
    })
  }

,并且在UI中声明了filepond UI实例,如下所示:

const { ....,avatar,....} = this.state;

<FilePond 
  ref={ref => this.pond = ref}
  files={avatar}
  fileValidateTypeLabelExpectedTypes="Invalid File Type"
  imagePreviewMaxFileSize='3MB'
  imageValidateSizeMinWidth="400"
  imageValidateSizeMaxWidth="1024"            
  imageValidateSizeMinHeight="400"
  imageValidateSizeMaxHeight="1024"
  imagePreviewTransparencyIndicator="green"
  maxFileSize='3MB'
  labelIdle={'Drag & Drop your Profile Picture or <span class="filepond--label-action"> Browse </span>'}
  acceptedFileTypes={['image/png', 'image/jpeg']}
  onupdatefiles={fileItems => {
    this.setState({
      avatar: fileItems.map(fileItem => fileItem.file)
    });
  }}              
/>

但是我得到了预期的Blob或文件错误... 我该如何解决?

1 个答案:

答案 0 :(得分:0)

在将avatar值发送到存储之前,它有助于记录该值。您的存储空间告诉您它不是Blob还是File

在这种情况下,看起来avatar是一个数组。

this.setState({
    avatar: fileItems.map(fileItem => fileItem.file) // this is an array
});

因此,您可以使用avatar选择数组中的第一个元素,而不是使用整个数组的avatar[0]

const upload = storage.ref(`avatars/${credential.user.uid}`).put(avatar[0])