Reputation: 688
I'm trying to set up a react native project with firebase, without much success. The config data is fetched from here: https://console.firebase.google.com/project/my-project/overview, so everything there is generated automatically.
Here's my code for the setup:
const config = {
apiKey: "[MY-API-KEY]",
authDomain: "[MY-AUTH-DOMAIN]",
databaseURL: "[MY-DATABASE-URL]",
storageBucket: "[MY-STORAGE-BUCKET]"
}
const firebaseApp = firebase.initializeApp(config);
const usersRef = firebaseApp.database().ref('Users');
and later, at some point, if I try to run this:
usersRef.set({ test: "sdfsdf" });
nothing happens in the DB. I also can't get data from the database...
Any clue what I'm doing wrong in here?
Upvotes: 1
Views: 2472
Reputation: 8686
I've encountered a similar issue when importing my database
reference object from another file. Basically, when you're performing the firebase app initialization in the same file than where you perform the on
or set
methods, that works. But once you try to import the database
from another file, the config seems lost...I think it has to do with how ReactNative bundles the javascript, or something like that. Here's a workaround I came with :
database.js
import * as Firebase from 'firebase'
let HAS_INITIALIZED = false
const initFirebase = () => {
if (!HAS_INITIALIZED) {
const config = {
apiKey: "**************",
authDomain: "************",
databaseURL: "*********",
storageBucket: "*********",
}
Firebase.database.enableLogging(true)
Firebase.initializeApp(config)
HAS_INITIALIZED = true
}
}
export const getDatabase = () => {
initFirebase()
return Firebase.database()
}
then, in whatever-file-you-want.js
import { getDatabase } from './database'
getDatabase().ref('...') // etc.
Upvotes: 2