【发布时间】:2017-06-21 01:47:19
【问题描述】:
我正在尝试从我的 firebase 中读取所有数据 但我不能这样做
这是我的代码:
firebase.database().ref().child('users/').on('value',function(snapshot) {
window.alert(snapshot.val());
});
我的火力基地看起来像这样:
"users":
{
"uid": {
"username": "AAA",
"email": "aaa@a.com"
},
"uid": {
"username": "ZZZ",
"email": "zzz@z.com"
}
}
我无法使用此代码获取任何数据
更新
我已经在 firebase 中定义了我的安全性
{
"rules":{
"users":{
"$uid": {
// grants write access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth !== null && auth.uid === $uid ",
//".write": "auth !== null && (auth.uid === $uid || auth.uid ==='0127a226-83ec-4eae-bbc7-53b81da10c0d')",
// grants read access to any user who is logged in with an email and password
".read": "auth !== null && auth.uid ===$uid "
//".read": "auth !== null && (auth.uid ===$uid || auth.uid ==='0127a226-83ec-4eae-bbc7-53b81da10c0d')"
//".validate": "newData.hasChildren(['full_name','uid','state_light'])"
}
}
} }
【问题讨论】:
-
查看我的答案,如果不能解决问题,您的初始 firebase 参考是如何定义的?
-
现在在 console.firebase.google.com 上创建的新 Firebase 数据库要求用户在读取数据之前进行身份验证。很可能您没有读取数据的权限。一种快速查看是否是原因的方法是,将错误处理程序添加到您的
on()调用中:on('value', ..., function(error) { console.error(error); }并检查浏览器的 JavaScript 控制台。 -
它显示错误,我在线程中发布我的安全性
-
您可以删除您的规则并将主规则节点下的 .read 和 .write 都设置为 true 吗?这将验证规则是否导致问题。
-
我认为不是规则问题,因为我有应用程序使用相同的规则,但它可以正确读/写
标签: javascript firebase firebase-realtime-database