buka korek
buka korek

Reputation: 59

ErrorType: database is not a function Firebase

I want connect with my database and when I write this code I get this error:

TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_1__.default.database is not a function

    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"

},

How can I resolve this problem?

Upvotes: 2

Views: 2739

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598668

The only import you currently have for Firebase is:

import firebase from "firebase/app"

This imports only the basic SDK that allows you to initialize the firebase object. It does not import any product-specific SDKs.

To use the Realtime Database, you also need to import that SDK with:

import "firebase/database";

Upvotes: 3

Related Questions