¿Estoy usando un código obsoleto? Además, establecí las reglas de la base de datos para que la lectura y la escritura sean verdaderas. Eso hace que la base de datos se pueda cambiar desde cualquier dispositivo. Cuando intento buscar en el desbordamiento de pila, firebase no está definido, obtengo respuestas irrelevantes.
<!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>Tienes que usar la función getDatabase que importaste aquí:
import { getDatabase } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-database.js"; Entonces, puede hacer const db = getDatabase() , y acceder a su tiempo real usando eso.
Su fragmento con la corrección:
<!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>Probablemente no podrá hacer esto dentro de los fragmentos de StackOverflow porque se ejecuta en un iFrame
También puede consultar los documentos aquí: https://firebase.google.com/docs/database/web/read-and-write