Im following this video right here.
And here's my codes.
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
})();
Output
BTW
Here's the structure of my test project in case maybe it matters.
fire-test
|_ index.html
|_ app.js
And im running it on apache
under /var/www/html/fire-test
http://localhost/fire-test/
Actually firebase's database has it's authentication and I found out that the rules on my database is not set properly (to public).
I changed this :
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
to
{
"rules": {
".read": true,
".write": true
}
}
"auth != null"
to true
Note
Setting the rules this way is a bad idea. We don't want anyone to access the root
node. Though in this answer, this is just to test out the firebase connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With