带有react的firebase.firestore没有创建Firebase应用'[DEFAULT]'-调用Firebase App.initializeApp()(app / no-app)

时间:2018-07-14 15:16:15

标签: reactjs firebase google-cloud-firestore

尝试对用户firebase.firestore做出反应。这是我初始化firebase的方法。

import React, { Component } from 'react'

import { Provider } from 'react-redux'
import store from './redux'

import Routes from './Routes'

import initFirebase from './initFirebase'

class App extends Component {
  componentWillMount () {
    initFirebase()
  }

  render () {
    return <Provider store={store}>
      {Routes}
    </Provider>
  }
}

export default App

这是initFirebase.js

import firebase from 'firebase'
import store from './redux'
import { setAuth } from './redux/actions/auth'

export default () => {
  // Initialize Firebase
  var config = {
    apiKey: 'AIzaSyC1H97dDoIVurLdgHOGgfRRubrTmb3YkTo',
    authDomain: 'tree-of-life-ee870.firebaseapp.com',
    databaseURL: 'https://tree-of-life-ee870.firebaseio.com',
    projectId: 'tree-of-life-ee870',
    storageBucket: 'tree-of-life-ee870.appspot.com',
    messagingSenderId: '861153906067'
  }
  firebase.initializeApp(config)

  firebase.auth().onAuthStateChanged(user => {
    store.dispatch(setAuth(user))
  })
}

在这里,我尝试像这样使用firestore

import firebase from 'firebase'
import { SubmissionError } from 'redux-form'

import { browserHistory } from 'react-router'

const db = firebase.firestore()

并得到这样的错误

Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).

那么在反应环境中初始化firebase的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您需要在调用firebase.firestore()之前初始化firebase 您可以移动

import firebase from 'firebase';
firebase.initializeApp(config);

index.js文件。