【问题标题】:react native firebaseURL not found未找到本机 firebaseURL 的反应
【发布时间】:2020-05-02 07:01:27
【问题描述】:

在 react native 中将一些数据推送到 firebase 时,我收到以下错误,即我的 firebase 数据库 URL 未定义。我在这里使用安卓模拟器。也许我错过了我需要在我的 android 构建 gradle 文件中推送到 firebase 的东西?

我目前也在为这个应用程序使用 firebase auth,它确实工作得很好,但我不得不修改 android build gradle。我想知道这是否可能是一个类似的问题?如果是这样,有人知道我需要添加什么吗?

我是 react-native 的新手抱歉:(

下面是我的配置和我的 firebase 应用初始化

sendNoteToDatabase = () => {
  const firebaseConfig = {
    apiKey: 'my api key',
    authDomain: '*********-37824.firebaseapp.com',
    databaseURL: 'https://******-37824.firebaseio.com/',
    projectId: '******-23288'
  }

  if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfig);
  }

  const note = {
    title: "this.state.title",
    who: "this.state.who",
    time: "this.state.time"
  }

  firebase.database().ref('Users/').set(note).then((data) => {
    //success callback
    console.log('data ', data)
  }).catch((error) => {
    //error callback
    console.log('error ', error)
  })
}

我已经检查了 URL,并且我正在使用实时数据库。我不确定这是我的代码结构的问题还是android的问题?

【问题讨论】:

  • 您的应用程序中的某些东西似乎在初始化之前尝试使用数据库。错误中的堆栈跟踪将告诉您确切的来源。
  • 谢谢!这帮助我意识到我需要使用挂钩来初始化应用程序

标签: android firebase react-native firebase-realtime-database react-native-firebase


【解决方案1】:

使用 Franks Advice 我能够通过使用 componentDidMount 挂钩初始化 firebase 应用程序来解决它


componentDidMount(){
    if (!firebase.apps.length) {
      firebase.initializeApp(firebaseConfig);
    }
  }

【讨论】:

    猜你喜欢
    • 2016-12-08
    • 2021-11-08
    • 2017-06-28
    • 2019-11-02
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    相关资源
    最近更新 更多