Firebase—Uncaught TypeError: database. error.Ref不是一个函数



如果这不是一个好问题,我很抱歉(我对Firebase和JavaScript编程相当陌生)。

我一直试图得到一个相对简单的程序启动和运行。具体来说,当我运行我的代码,我想一个"开始"按钮出现。一旦用户点击这个按钮,它将增加一个玩家的数量,并相应地更新我的Firebase实时数据库中的playerCount。

我注意到当我运行这个程序时,我得到一个错误:"Uncaught TypeError: database。Ref不是一个函数">

这是我的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/phaser@3.11.0/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);

我怀疑我可能没有正确设置浏览器模块,但我不完全确定。任何帮助将非常感激!

database.ref('/')这似乎不对。

通常,您将查找名称为database.ref('game/')的根节点。