firebase@3.3.0

react-native v0.32台通过wifi在安卓设备上测试

Firebase数据库没有任何身份验证规则,它是开放读写的.

考虑到以下文件 struct :

|_ firebase.js
|_ actions.js

这不管用:

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

没有别的了...


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  

我做错了什么?我还注意到,一旦输入import firebase from 'firebase'firebase变量在所有文件中全局可用,这些文件不是import语句中的firebase变量(我本可以写import FooBar from 'firebase'firebase全局变量仍然被导入)

推荐答案

因为似乎没有人有"官方"的答案.下面是我用来提供某种惰性初始化的变通方法:

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
}

然后,在任何需要database的地方:

import { getDatabase } from './firebase'

const methodThatNeedDatabase = () => {
    getDatabase().ref('/some/ref')
    ...
}

React-native相关问答推荐

如何在API调用期间将本地存储(Expressc存储)中的令牌传递到后台

在我的 React Native 应用程序中获取数据返回 [object Object] 而不是 renderSectionHeader

如何渲染一个 svg 以在不同的窗口大小下动态地看起来相同?

使用 Auth Token 标头react 本机图像,如何处理 401?

你使用 react native 应该学习 react.js 吗?

react-native-snap-carousel 无法正确渲染

React native flexbox - 如何做 percentages || columns || responsive || grid etc

使用 AsyncStorage 如何以及在何处保存整个 redux 存储

使用 Hooks 在功能组件中react-navigation 标题

无法访问函数内部的状态

React-Native 应用程序的 Jest 测试 Animated.View

React Native - NSNumber 无法转换为 NSString

React Native - 用分机拨打电话号码

如何从 stack.navigation 外部的组件中使用 navigation.navigate

react native如何访问文件系统?

带有 xcode 模拟器的 react-native 有非常慢的alert

未计算文件的 React-Native Bundle Error 错误 SHA-1

iOS ActionSheet 的 Android 类似功能

Gradlew bundleRelease 不会在 react-native 中生成发布 apk

如何在react-native中更新数组状态?