Firebase .on(“价值”)不起作用 [英] Firebase .on('value') not working

查看:156
本文介绍了Firebase .on(“价值”)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林以下此视频此处



以下是我的代码。
$ b 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>

<! - 值 - >
< pre id =object>< / pre>

< script src =app.js>< / script>
< / body>
< / html>

app.js



'pre> (函数(){

//初始化火力地堡
变种配置= {
apiKey: AIzaSyCOJZqfas4gxwEYBbRNyyIy7Z9vEsTx4ME,
authDomain: fire-test-e2185.firebaseapp.com,
databaseURL: https://fire-test-e2185.firebaseio.com,
storageBucket:火试验e2185 .appspot.com的,
};

firebase.initializeApp(配置);

变种preObject =的document.getElementById( '对象');

$ b $ //创建引用
var dbRefObject = firebase.database().ref()。child('object')

console.log('test (); //记录

//同步对象变化
dbRefObject.on('value',function(snap){
console.log(snap.val() ); //不记录
));

console.log('test log'); //记录
})();

输出


$ b 这是我的测试项目的结构,以防万一。

  fire-test 
| _ index.html
| _ app.js

然后在下使用 apache / var / www / html / fire-test
$ b

http:// localhost / fire-test /

我改变了这个:

  {
rules :{
.read:auth!= null,
.write:auth!= null
}
}

  {
规则:{
.read:true,
.write:true
}
}

auth!= null to true

Tough Im很确定这应该是基于firebase的标准以编程方式完成的,这只是为了测试连接的目的。

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

Tough Im pretty sure that this should be done programmatically base on firebase's standards, this is just for the purpose of testing out the connection.

这篇关于Firebase .on(“价值”)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆