使用react-native-fetch-blob将图像上传到React Native中的Firebase存储时出错

时间:2018-07-17 11:41:04

标签: firebase react-native react-native-android firebase-storage react-native-fetch-blob

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

它也警告我possible unhandled promise Rejection (id=0):undefined类型不是一个功能(评估'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"

}

错误提示:enter image description here

警告提示:enter image description here

1 个答案:

答案 0 :(得分:1)

如果您有rn-fetch-blob库,则无需库react-native-image-picker即可将图像上传到Firebase。

此错误的根本原因是:

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

它是从react-native-firebase库中触发的,因为您将错误的参数数据传递给了imageRef.put(blob ...库中生成的blob。{p}

您无需使用rn-fetch-blob库中的blob,而是只需要传递uri数据即可。让我告诉你如何。

rn-fetch-blob

有关错误处理,您可以参考官方的Firebase存储文档here