Having issues uploading files to Firebase using the react-firebase-file-uploader package.
The error received is Uncaught (in promise) TypeError: storageRef.child is not a function
Firstly, firebase is imported into my GatsbyJS Layout.js in componentDidMount as
Layout.js
componentDidMount() {
const app = import('firebase/app');
const auth = import('firebase/auth');
const database = import('firebase/firestore');
const storage = import('firebase/storage');
Promise.all([app, auth, database, storage]).then(values => {
const firebase = getFirebase(values[0]);
!this.isCancelled && this.setState({ firebase });
});
}
Then, firebase is added as props using React.createContext to pass functions to components as
FirebaseContext.js
const FirebaseContext = React.createContext(null);
export const withFirebase = Component => props => (
<FirebaseContext.Consumer>
{firebase => <Component {...props} firebase={firebase} />}
</FirebaseContext.Consumer>
);
export default FirebaseContext;
Inside the firebase component, we use onboardStorage as a function containing this.storage as
Firebase.js
class Firebase {
constructor(app) {
app.initializeApp(config);
/* Firebase APIs */
this.app = app;
this.storage = app.storage();
// *** Storage API ***
onboardStorage = () => this.storage;
}
let firebase;
function getFirebase(app, auth, database, storage) {
if (!firebase) {
firebase = new Firebase(app, auth, database, storage);
}
return firebase;
}
export default getFirebase;
Which is then passed to the FileUploader component in my form component as
Form.js
<FileUploader
accept="image/*"
name="avatar"
filename="operator_accreditation"
storageRef={() => firebase.onboardStorage().ref(`${uid}/files`)}
onUploadStart={this.handleUploadStart}
onUploadError={this.handleUploadError}
onUploadSuccess={this.handleUploadSuccess}
onProgress={this.handleProgress}
/>
each of the handle functions in Form.js are as follows
handleUploadStart = () =>
this.setState({
operatorAccreditationIsUploading: true,
operatorAccreditationProgress: 0
});
handleProgress = operatorAccreditationProgress =>
this.setState({ operatorAccreditationProgress });
handleUploadError = error => {
this.setState({ operatorAccreditationIsUploading: false });
console.error(error);
};
handleUploadSuccess = filename => {
const { firebase, uid } = this.props;
this.setState({
operatorAccreditation: filename,
operatorAccreditationProgress: 100,
operatorAccreditationIsUploading: false
});
const storageOperatorAccreditation = firebase.onboardStorage();
storageOperatorAccreditation
.ref(`${uid}/files`)
.child(filename)
.getDownloadURL()
.then(url => this.setState({ operatorAccreditation: url }));
};
const uid is the the auth.uid
Adding a console message indicates that none are triggered on uploading a file.
The error occurs after a file is selected, indicating that the issue is at the storageRef of the FileUploader. What have I done wrong to get Uncaught (in promise) TypeError: storageRef.child is not a function?