I am developing an small React Native POC with firebase storage in which a user selects image and uploads it to firebase storage under /uid/ directory.
I am using react-native-firebase for using firebase in the application.
I have the uri of the image with which i am creating its blob using RNFetchBlob and then trying to upload the blob to firebase.
Here is the code for handling the blob making and uploading.
handleUploadTask = (mime = 'application/octet-stream') => {
const {imageSource} = this.state;
this.setState({uploading: true});
const Blob = RNFetchBlob.polyfill.Blob;
const fs = RNFetchBlob.fs;
fs.readFile(imageSource.uri, 'base64')
.then((data) => {
return Blob.build(data, { type: `${mime};BASE64` })
}).then(blob => {
console.log('going to upload');
this.uploadBlobToFirebase(blob);
}).catch(error => {
console.error('fs error:', error);
})
}
and here is the uploadBlobToFirebase
uploadBlobToFirebase = (blob) => {
const currentUserId = firebase.auth().currentUser.uid;
const path = `/${currentUserId}/`;
const unsubscribe = firebase.storage().ref(path).putFile(blob).on(
firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
//restricting progress to be shown with only 0,2,4,... 98, 100.
if (progress % 2 == 0) {
this.setState({
uploadProgress: progress
})
}
if (snapshot.state === firebase.storage.TaskState.SUCCESS) {
this.setState({
uploading: false,
}, () => {
//TODO: should also add this image to flatlist.
//Get download url and set to image in flatlist.
Alert.alert('Upload completed');
})
}
},
(error) => {
this.setState({
uploading: false,
}, ()=>{
unsubscribe();
console.error(error);
})
},
);
}
The function handleUploadTask is called on an onPress of a upload button and runs fine till console.log('going to upload') and then ends up into the catch with error:
'fs error:', { [TypeError: undefined is not a function (evaluating 'filePath.replace('file://', '')')] line: 115903, column: 41, sourceURL: 'http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false' }
I am totally blank here. Not able to find the file.replace which is causing issue and why.
Any help would be appreciated. Thanks.