Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase .on('value') not working

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

enter image description here

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/

like image 233
CENT1PEDE Avatar asked Aug 04 '16 17:08

CENT1PEDE


1 Answers

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.

like image 61
CENT1PEDE Avatar answered Oct 14 '22 06:10

CENT1PEDE