最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase .on('value') not working - Stack Overflow

programmeradmin6浏览0评论

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/

Share Improve this question asked Aug 4, 2016 at 17:53 jofftiquezjofftiquez 7,70810 gold badges70 silver badges122 bronze badges 5
  • 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
Add a ment  | 

2 Answers 2

Reset to default 15

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'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.

发布评论

评论列表(0)

  1. 暂无评论