0

使用 firebase 和 react-native-fetch-blob 库创建 React-Native 应用程序。由于此错误,我无法将图像上传到 firebase java.lang.String com.facebook.react.bridge.ReadableMap.string(java.lang.string) on a null object Reference

它还给了我警告possible unhandled promise Rejection (id=0):类型未定义不是一个功能(评估'filepath.replace('file://,')')尝试这个react-native-fetch-blob库代码:

function uploadImage() {

        return new Promise((resolve, reject) => {
            let imgUri = 'content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F80/ORIGINAL/NONE/1685675380'; let uploadBlob = null;
            const uploadUri = imgUri;

            const imageRef = firebase.storage().ref();
            const name = 'myimg';
            mime = 'image/jpeg';
            fs.readFile(uploadUri, 'base64')
                .then(data => {
                    return Blob.build(data, { type: `${mime};BASE64` });
                })
                .then(blob => {
                    uploadBlob = blob;
                    return imageRef.put(blob, { contentType: mime, name: name });
                })
                .then(() => {
                    uploadBlob.close()
                    return imageRef.getDownloadURL();
                })
                .then(url => {
                    resolve(url);
                })
                .catch(error => {
                    reject(error)
                })
        })
    }

dependencies": {
"fbjs": "^0.8.16",
"react": "^16.3.1",
"react-native": "^0.55.3",
"react-native-firebase": "^4.1.0",
"react-native-image-picker": "^0.26.10",
"react-navigation": "^2.6.0",
"rn-fetch-blob": "^0.10.11"

}

错误截图:在此处输入图像描述

警告截图:在此处输入图像描述

4

1 回答 1

1

如果您有库,则不需要库rn-fetch-blob将图像上传到 firebase react-native-image-picker

此错误的根本原因来自:

type undefined is not a funtion (evaluating 'filepath.replace('file://,")')..

它是从库的某个地方触发的,react-native-firebase因为您将错误的参数数据传递给imageRef.put(blob ... 从库生成的 blob rn-fetch-blob

而不是使用blob来自rn-fetch-blob库的,你只需要传递一个 uri 数据,它应该可以工作。让我告诉你怎么做。

//when user pick an image from local storage
ImagePicker.showImagePicker(options, (response) => {
  if (response.didCancel) {
    console.log('User cancelled photo picker');
  }
  else if (response.error) {
    console.log('ImagePicker Error: '+response.error);
  }
  else if (response.customButton) {
    console.log('User tapped custom button: '+response.customButton);
  }
  else {
    firebase.storage().ref().child('myimg.jpg')
    .put(response.uri, { contentType : 'image/jpeg' }) //--> here just pass a uri
    .then((snapshot) => {
      console.log(JSON.stringify(snapshot.metadata));
    })
  });
}

对于错误处理,您可以在此处参考官方 Firebase 存储文档

于 2018-08-30T05:44:30.270 回答