I am trying to use firebase for my chat application developed using React Native and I am getting following error while pushing records into firebase.
[TypeError: _firebaseConfig.default.database is not a function. (In '_firebaseConfig.default.database()', '_firebaseConfig.default.database' is undefined)]
I have created a firebase configuration file as below.
firebase-config.js
import Firebase from 'firebase/compat/app';
const firebaseConfig = {
apiKey: '',
databaseURL: '',
projectId: '',
appId: '',
};
export default Firebase.initializeApp(firebaseConfig);
Messages.js
import Firebase from './firebase-config';
export const sendMessage = async (from, to, message) => {
try {
return await Firebase.database()
.ref('messages/' + from)
.child(to)
.set({
from: from,
to: to,
message: message,
});
} catch (error) {
console.log(error);
}
};
export const receiveMessage = async (from, to, message) => {
try {
return await getDatabase(Firebase)
.ref('messages/' + to)
.child(from)
.push({
from: from,
to: to,
message: message,
});
} catch (error) {
console.log(error);
}
};
You're not importing the Realtime Database SDK anywhere, so when you try to then access firebase.database() you get an error saying that it can't be found.
To fix this, import the Realtime Database SDK after you import Firebase from 'firebase/compat/app'.
import firebase from 'firebase/compat/app';
import 'firebase/database';
...
I recommend also checking out the example in the upgrade guide on updating imports to v9 compat.