如何在React Native中修复“ _filePath.includes不是函数。(在'_filePath.includes('&')中,'_ filePath.includes'未定义)”?

时间:2019-06-28 14:18:38

标签: firebase-storage

我正在尝试将图像上传到Firebase Storage,但是,ref.putfile()导致标题错误

我没有找到与此错误相关的任何适当资源

这是我从用户那里获得图像的地方:

openPicker = () =>  {

    // More info on all the options is below in the API Reference... just some common use cases shown here
    const options = {
      title: 'Fotoğraf Seç',
      storageOptions: {
        skipBackup: true,
        path: 'images',
      },
    };
    ImagePicker.showImagePicker(options, (response) => {
      console.log('Response = ', response);

      if (response.didCancel) {
        console.log('User cancelled image picker');
      }
      else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      }
      else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
      }
      else {
        const source = { uri: response.uri}
        this.setState({
          imageMessageSrc: source
        });
        this.uploadImage();
      }
    });

  }

然后我尝试将图像上传到Firebase

 uploadImage = () => {
    console.log("Here");
    const filename = this.randIDGenerator// Generate unique name
    firebase
      .storage()
      .ref(`${firebase.auth().currentUser.uid}/sentPictures/${filename}`)
      .putFile(this.state.imageMessageSrc)
      .then(() => {
        console.log("Here1");
      })
      .catch((error) => {
        console.log(error);
      })

当我删除putFile时,错误消失了,但是显然数据库没有任何反应。

1 个答案:

答案 0 :(得分:2)

问题与filePath和fileUri之间的差异有关。因此,解决方案如下:

    openPicker = () => {
            const options = {
                title: 'Fotoğraf Seç',
                storageOptions: {
                    skipBackup: true,
                    path: 'images',
                    allowsEditing: true,
                },
            };

            ImagePicker.showImagePicker(options, (response) => {
                console.log('Response = ', response);

                if (response.didCancel) {
                    console.log('User cancelled image picker');
                }
                else if (response.error) {
                    console.log('ImagePicker Error: ', response.error);
                }
                else if (response.customButton) {
                    console.log('User tapped custom button: ',response.customButton);

                }
                else {
                    var path = '';
                    if (Platform.OS == 'ios')
                        path = response.uri.toString();
                    else {
                        path = response.path.toString();
                    }
                    const image= {
                        image: response.uri.toString(),
                        path: path
                    };
                    this.uploadImage(image);
                }
            });
        }
     uploadImage = (image) => {
           firebase.storage().ref(uploadUrl).putFile(image.path);
}

我意识到 Firebase Storage putFile 函数不适用于图像uri,而应该随filePath一起提供。我使用此图片的uri甚至在上传之前就直接在屏幕上显示了图片。