我在这里关注这个视频。
这是我的代码。
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
我在apache
下面运行/var/www/html/fire-test
http://localhost/fire-test/
实际上,firebase的数据库已通过身份验证,我发现数据库上的规则未正确设置(公开)。
我改变了这个:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
到
{
"rules": {
".read": true,
".write": true
}
}
"auth != null"
到 true
笔记
以这种方式设置规则不是一个好主意。我们不希望任何人访问该root
节点。尽管在此答案中,这只是测试Firebase连接。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句