【发布时间】:2019-11-15 22:17:28
【问题描述】:
我的数据库结构是。
Homeland
+Ui4PSHU0kCPGpFJ6jWIJPOdLvEds
+service
+quta
+period
+55dwaSHU0kCPGpFJ6jWIJPOdLvEds
+service
+quta
+period
我想公开“服务”和“quta”孩子,但只有注册用户才能显示“句号”孩子。但是我的用户目录存在问题。
首先我想测试我的 $uid 是否可以。但我不能。当我删除到 $uid 时它可以工作,只使用“Homeland”。所以它没有工作
{
"rules": {
"Homeland": {
"$uid" : {
".read": true,
".write": "auth !== null"
},
},
".read": "auth !== null",
".write": "auth !== null"
}
}
这是工作
{
"rules": {
"Homeland": {
".read": true,
".write": "auth !== null"
},
".read": "auth !== null",
".write": "auth !== null"
}
}
更新
按照 samthecodingman 所述更改我的安全规则。
{
"rules": {
"Homeland": {
"$uid" : {
"service": {
".read": true, // <-- publically readable
".write": "auth.uid === $uid" // <-- writable only by owner
},
"quta": {
".read": true, // <-- publically readable
".write": "auth.uid === $uid" // <-- writable only by owner
},
"period": {
".read": "auth !== null", // <-- readable by any logged in user
".write": "auth.uid === $uid" // <-- writable only by owner
}
},
},
"$other": { // <-- everywhere else not named above
".read": "auth !== null", // <-- allows read if logged in
".write": "auth !== null" // <-- allows write if logged in
}
}
}
代码
var kullanici = firebase.database().ref().child("Homeland").child(uservaluefrominput).child("service");
kullanici.on('value' ,function(datasnapshot) {
sifre = datasnapshot.val();
console.log(sifre);
});
但是代码和模拟都被拒绝读取值。
抓住错误。注意:这次我声明了用户标识。
try {
kullanici = firebase.database().ref().child("Homeland").child("Ui4PSHU0kCPGpFJ6jWIJPOdLvEO2").child("service");
kullanici.on('value' ,function(datasnapshot) {
sifre = datasnapshot.val();
console.log(sifre);
});
} catch (someError) {
console.log('got some error: ', someError);
}
【问题讨论】:
-
请将触发问题的代码添加到您的问题中。没有代码规则是毫无意义的。
-
在
var kullanici = ...下面添加kullanici.on('value', (s)=>console.log(s), (err) => console.error(err))行会出现什么错误? -
我不确定如何使用你的代码,我使用了 try-catch 方法,但是我在控制台上什么也没有,请在第一条消息上检查我的代码。
-
try and catch 不会像您期望的异步代码那样工作。我在上面的注释中包含的那行是一个调试功能。将其按原样插入
kullanici = firebase.database().ref()...下方的行,然后运行您的代码。它所做的只是从on('value', ...)获取响应并将其转换为控制台中的记录消息。然后复制这里的任何错误 -
"kullanici.on('value', (s)=>console.log(s), (err) => console.error(err)) ,function(datasnapshot) { " 这一行给了我一个 SyntaxError: unexpected token: ')'
标签: firebase firebase-realtime-database firebase-security