Home > other >  firebase is not defined (using version 9.6.7)
firebase is not defined (using version 9.6.7)

Time:02-22

Am I using outdated code? Also, I set the rules of the database so read and write are true. That makes the database changeable from any device. When I try searching on stack overflow, firebase is not defined, I get irrelevant answers.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module">
        // Import the functions you need from the SDKs you need
        import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-app.js";
        import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-analytics.js";
        import { getDatabase } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-database.js";
        // TODO: Add SDKs for Firebase products that you want to use
        // https://firebase.google.com/docs/web/setup#available-libraries
      
        // Your web app's Firebase configuration
        // For Firebase JS SDK v7.20.0 and later, measurementId is optional
        const firebaseConfig = {
          apiKey: "AIzaSyCnfB3Tfu9bo7PyEvhB1ZAPBbit7ZWm9D8",
          authDomain: "square-1cdce.firebaseapp.com",
          projectId: "square-1cdce",
          storageBucket: "square-1cdce.appspot.com",
          messagingSenderId: "82191280296",
          appId: "1:82191280296:web:45e62faffbb7e8a94cfa9d",
          measurementId: "G-NT53HS3WE9"
        };
      
        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
        const analytics = getAnalytics(app);
        </script>
        <script>
        const db = firebase.database()
        db.ref("users/").set({
            user:"hello"
        });
      </script>
</body>
</html>

CodePudding user response:

You have to use the getDatabase function you imported here:

import { getDatabase } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-database.js";

So, you can do const db = getDatabase(), and access your realtime using that.

Your snippet with the fix:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module">
        // Import the functions you need from the SDKs you need
        import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-app.js";
        import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-analytics.js";
        import { getDatabase, ref, set } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-database.js";
        // TODO: Add SDKs for Firebase products that you want to use
        // https://firebase.google.com/docs/web/setup#available-libraries
      
        // Your web app's Firebase configuration
        // For Firebase JS SDK v7.20.0 and later, measurementId is optional
        const firebaseConfig = {
          apiKey: "AIzaSyCnfB3Tfu9bo7PyEvhB1ZAPBbit7ZWm9D8",
          authDomain: "square-1cdce.firebaseapp.com",
          projectId: "square-1cdce",
          storageBucket: "square-1cdce.appspot.com",
          messagingSenderId: "82191280296",
          appId: "1:82191280296:web:45e62faffbb7e8a94cfa9d",
          measurementId: "G-NT53HS3WE9"
        };
      
        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
        const analytics = getAnalytics(app);
        const db = getDatabase()
        set(ref(db, 'users/'), {
          hello: 'world'
        });

        </script>
</body>
</html>

You probably won't be able to do this inside StackOverflow snippets because it runs in an iFrame

You can also refer to the docs here: https://firebase.google.com/docs/database/web/read-and-write

  • Related