反应本地firebaseURL未找到

时间:2020-01-15 13:41:50

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

在以响应本机将一些数据推送到firebase时,出现以下错误,即我的firebase数据库URL未定义。我在这里使用android模拟器。也许我缺少一些需要在我的android build gradle文件中推送到Firebase的东西?

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

我是新来的本机抱歉:(

Joshua Lamson realm Talk

下面是我的配置和我的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问题?

1 个答案:

答案 0 :(得分:1)

使用Franks建议,我可以使用componentDidMount钩子初始化firebase应用程序来解决它


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