我正在关注此视频 here .
这是我的代码。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Fire Test</title>
<script src="https://www.gstatic.com/firebasejs/3.2.1/firebase.js"></script>
</head>
<body>
<!-- Value -->
<pre id="object"></pre>
<script src="app.js"></script>
</body>
</html>
app.js
(function () {
// Initialize Firebase
var config = {
apiKey: "AIzaSyCOJZqfas4gxwEYBbRNyyIy7Z9vEsTx4ME",
authDomain: "fire-test-e2185.firebaseapp.com",
databaseURL: "https://fire-test-e2185.firebaseio.com",
storageBucket: "fire-test-e2185.appspot.com",
};
firebase.initializeApp(config);
var preObject = document.getElementById('object');
// Create reference
var dbRefObject = firebase.database().ref().child('object')
console.log('test log'); // logging
// Sync object changes
dbRefObject.on('value', function (snap) {
console.log(snap.val()); // not logging
});
console.log('test log'); // logging
})();
输出
顺便说一下
以下是我的测试项目的结构,以防万一。
fire-test
|_ index.html
|_ app.js
我在 /var/www/html/fire-test
下的 apache
上运行它
http://localhost/fire-test/
最佳答案
实际上 firebase 的数据库有它的身份验证,我发现我的数据库的规则没有正确设置(公开)。
我改变了这个:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
到
{
"rules": {
".read": true,
".write": true
}
}
"auth != null"
到 true
注意
以这种方式设置规则不是一个好主意。我们不希望任何人访问 root
节点。虽然在这个答案中,这只是为了测试 firebase 连接。
关于javascript - Firebase .on ('value' ) 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38774247/