【问题标题】:ErrorType: database is not a function FirebaseErrorType:数据库不是 Firebase 的函数
【发布时间】:2021-02-12 05:07:08
【问题描述】:

我想连接我的数据库,当我编写这段代码时,我得到了这个错误:

TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_1__.default.database 不是函数

    createNote() {
    if (this.state.title !== '' && this.state.note !== '') {
        firebase.database().ref('notes').push({       (probably in this line is error console say)
            title: this.state.title,
            note: this.state.note
        })
    }
}

index.js:

    import firebase from "firebase/app";

    const firebaseConfig = {
    apiKey: "...",
........
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

package.js

 "dependencies": {
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.7.0",
"firebase": "^8.2.6",                     (I only this add)
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.2",
"web-vitals": "^1.1.0"

},

我该如何解决这个问题?

【问题讨论】:

  • 这取决于您如何导入 firebase(在您的代码中)以及您如何添加依赖项(可能是您的 package.json)文件。您可以编辑您的问题以显示两者吗?
  • 编辑了帖子,希望我把所有东西都放在这里。我也在 firebase 中使用实时数据库

标签: reactjs firebase jsx


【解决方案1】:

您目前对 Firebase 的唯一导入是:

import firebase from "firebase/app"

这仅导入允许您初始化 firebase 对象的基本 SDK。它不导入任何特定于产品的 SDK。

要使用实时数据库,您还需要通过以下方式导入该 SDK:

import "firebase/database";

【讨论】:

  • 是的,是的:D 谢谢。现在可以使用了
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
相关资源
最近更新 更多