如果這不是一個好問題,我深表歉意(我對 Firebase 和 JavaScript 編程相當陌生)。
我一直在嘗試啟動并運行一個相對簡單的程式。具體來說,當我運行我的代碼時,我希望出現一個“開始”按鈕。用戶單擊此按鈕后,它會將玩家數量增加 1,并相應地更新我的 Firebase 實時資料庫中的 playerCount。
我注意到當我運行這個程式時,我收到一個錯誤:“Uncaught TypeError: database.ref is not a function”
這是我的 index.html 代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script defer type="module" src="https://www.gstatic.com/firebasejs/9.1.3/firebase-app.js"></script>
<script defer type="module" src="https://www.gstatic.com/firebasejs/9.1.3/firebase-firestore.js"></script>
<script defer type="module" src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
<script defer type="module" src="game.js"></script>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
</body>
</html>
這是我的 game.js 代碼:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.1.3/firebase-app.js";
import { getDatabase } from 'https://www.gstatic.com/firebasejs/9.1.3/firebase-database.js';
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "apiKey information",
authDomain: "authDomain information",
databaseURL: "databaseURL information",
projectId: "projectId information",
storageBucket: "storageBucket information",
messagingSenderId: "messagingSenderId information",
appId: "appId information",
measurementId: "measurementId information"
};
const app = initializeApp(firebaseConfig);
// Store Firestore database in db
const database = getDatabase(app);
var player_num = 0;
// Display Start Button and increment player count
let button = document.createElement('button');
button.innerHTML = "Start";
button.onclick = function () {
player_num ;
database.ref('/').update({
playerCount: player_num
});
button.style.display = "none";
};
document.body.appendChild(button);
我懷疑我可能沒有正確設定瀏覽器模塊,但我不完全確定。任何幫助將非常感激!
uj5u.com熱心網友回復:
database.ref('/') 這似乎不對。
通常,您會查找名稱類似于database.ref('game/').
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329015.html
