20

[email protected]

react-native v0.32 tested on android device with wifi

Firebase database doesn't have any auth rules, it's open read and write.

Given the following file structure :

|_ firebase.js
|_ actions.js

This doesn't work :

firebase.js

import firebase from 'firebase'

const config = {
    apiKey: "*****",
    authDomain: "****",
    databaseURL: "*****",
    storageBucket: "*****",
}

firebase.database.enableLogging(true);

export default firebase.initializeApp(config)

actions.js

import firebase from './firebase'

export const fetchData = () => {
    const Data = firebase.database().ref('some/data')
    Data.on('value', (snapshot) => {
        console.log("snapshot", snapshot.val())  // never printed
    }, (error) => {
        console.error(error)
    })
}

debug output

p:0: Browser went online.  
firebase-database.js:36 p:0: Listen called for /some/data default  
firebase-database.js:36 p:0: Making a connection attempt

Nothing else...


This does work (but it's not a solution) :

firebase.js

...same content as above...

export default () => firebase.initializeApp(config)  // we export a function instead to trigger the initialization when the app is ready

actions.js

...same content as above...
const Data = firebase().database().ref('some/data') // we "manually" trigger the initialization, it's obviously not a good solution since we can't initialize the app multiple times

output

p:0: Browser went online.  
firebase-database.js:36 p:0: Listen called for /some/data default  
firebase-database.js:36 p:0: Making a connection attempt  
firebase-database.js:36 p:0: Auth token refreshed  
firebase-database.js:36 getToken() completed. Creating connection. 
firebase-database.js:36 c:0:0: Connection created  

What am I doing wrong here ? I also noticed that once I import firebase from 'firebase', the firebase variable is available globally in all the files that is NOT the firebase var from the import statement (I could have written import FooBar from 'firebase', the firebase global var is still imported)

5
  • Have you tried turn off debug mode? I had similar problem but it only happens in debug mode. Commented Aug 26, 2016 at 2:09
  • I actually did turned on the debug mode since nothing was happening, so unfortunately it's not that. Commented Aug 26, 2016 at 6:28
  • 2
    You're not doing anything wrong, something is broken between latest firebase and react-native (this might be normal but it doesn't seem documented anywhere). Worked for me with "firebase": "^3.1.0", "react": "15.2.1", "react-native": "^0.29.0". Commented Aug 31, 2016 at 11:04
  • OK thanks ! Not sure where I should post the issue then... Commented Aug 31, 2016 at 11:12
  • @Pcriulan you could try different dependency combination to see which one breaks first. I had actually started to but npm caching issues made it take way too long for the time I could dedicate to it. Commented Aug 31, 2016 at 13:07

1 Answer 1

2

Since nobody seems to have an "official" answer. Here is the workaround I came with to provide some sort of lazy initialization :

firebase.js

import Firebase from 'firebase'

let _database = null

const initFirebase = () => {
    var config = {
        apiKey: "*************",
        authDomain: "************",
        databaseURL: "**********",
        storageBucket: "************",
    }

    Firebase.database.enableLogging(true)
    Firebase.initializeApp(config)
}

export const getDatabase = () => {
    if (!_database) {
        initFirebase()
        _database = Firebase.database()
    }
    return _database
}

Then, anywhere you need the database:

import { getDatabase } from './firebase'

const methodThatNeedDatabase = () => {
    getDatabase().ref('/some/ref')
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Does this actually make it faster or just defer the delay?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.