firebaseを使ってウェブチャットを作りたい。
- var database = firebase.database();でエラーが出ます。
<!DOCTYPE html> <html lang="ja"> <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>webchat</title> </head> <body> <div> <div> <input type="text" id="name"> </div> <div> <textarea id="message" row="10"></textarea> <button id="send">send</button> </div> <div id="output"></div> </div> <script type="module"> // Import the functions you need from the SDKs you need import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.1/firebase-app.js"; import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.8.1/firebase-analytics.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: "AIzaSyCBZkDxfPA4F5T8yFN6nRN13cxXO13onl0", authDomain: "chat-e82eb.firebaseapp.com", projectId: "chat-e82eb", storageBucket: "chat-e82eb.appspot.com", messagingSenderId: "506878072817", appId: "1:506878072817:web:6c2b7abfeea16350209650", measurementId: "G-CTE5H5HRTT" }; // Initialize Firebase const app = initializeApp(firebaseConfig); const analytics = getAnalytics(app); </script> <script src="javascript/webchat.js"></script> </body> </html> **webchat.js** { var database = firebase.database(); let room = "chat_room"; const send = document.getElementById("send"); const name = document.getElementById("name"); const message = document.getElementById("message"); const output = document.getElementById("output"); send.addEventListener('click', function() { database.ref(room).push({ name:name.value, message:message.value }); message.value=""; name.value=""; }); database.ref(room).on("child_added", function(data) { const v = data.val(); const k = data.key; let str = ""; str += '<div class="name">名前:'+v.name+'</div>'; str += '<div class="text">メッセージ:'+v.message+'</div>'; output.innerHTML += str; }); }
不足情報があるなら言ってください。
申し訳ないです。。
あなたの回答
tips
プレビュー