【发布时间】: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 中使用实时数据库