Im following this video right here.
And here's my codes.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Fire Test</title>
<script src=".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",
databaseURL: "",
storageBucket: "fire-test-e2185.appspot",
};
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/
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./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.",
databaseURL: "https://fire-test-e2185.firebaseio.",
storageBucket: "fire-test-e2185.appspot.",
};
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/
- 1 without those basic rules in place. if you accidentally say db.ref().set('') you erase your entire db – Ronnie Smith Commented Aug 4, 2016 at 20:07
- @RonRoyston Ooh. I see, thanks!. Anyways, so If the basic rules is set to 'not public' how do i authenticate the request? – jofftiquez Commented Aug 4, 2016 at 20:11
-
You can't if read and write are set to
true
at the root. The default rules are setup so that only authenticated users have write access. You can make the rules very granular is you want to. – Ronnie Smith Commented Aug 4, 2016 at 20:16 - @RonRoyston I see. But how do you actually 'authenticate' the user. – jofftiquez Commented Aug 5, 2016 at 2:56
- Use the Firebase Authentication system. This might give you a head start - github./rhroyston/firebase-auth. – Ronnie Smith Commented Aug 5, 2016 at 2:58
2 Answers
Reset to default 15Actually 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're trying to just get a returned object containing the current values stored at that location in your firebase
db, change .on
to .once
.
Using .on
indicates that you want something to dynamically happen every time the argued event occurs, but .once
indicates that you just was the current data at that location.