firebase.default.database不是函数

我有这个firebase.js文件

import * as firebase from 'firebase/app'
var firebaseConfig = {
  apiKey: '***',
  authDomain: '***',
  databaseURL: '***',
  projectId: '***',
  storageBucket: '***',
  messagingSenderId: '***',
  appId: '***',
  measurementId: '***'
}

firebase.initializeApp(firebaseConfig)

export default firebase

And I've run npm i firebase on my project.

但是,当我尝试提交内容时,应用程序崩溃并显示以下错误:

TypeError:_firebase__WEBPACK_IMPORTED_MODULE_2 __。default.database不是函数

在我的App.js中,我像这样导入firebase变量

import firebase from './firebase'

这些是App.js内部的功能

  handleChange = event => {
    this.setState({
      [event.target.name]: event.target.value
    })
  }

  handleSubmit = event => {
    event.preventDefault()
    const itemsRef = firebase.database().ref('items')
    const toBeSafedItem = {
      title: this.state.currentItem,
      user: this.state.username
    }

    itemsRef.push(toBeSafedItem)
    this.setState({
      currentItem: '',
      username: ''
    })
  }

这是表格

            <form onSubmit={this.handleSubmit}>
              <input
                type='text'
                name='username'
                placeholder="What's your name?"
                onChange={this.handleChange}
                value={this.state.username}
              />
              <input
                type='text'
                name='currentItem'
                placeholder='What are you bringing?'
                onChange={this.handleChange}
                value={this.state.currentItem}
              />
              <button>Add Item</button>
            </form>
评论