gpt4 book ai didi

javascript - 如何使子节点的某些值 '.read' : true, 而其他值 '.read' : false?

转载 作者:行者123 更新时间:2023-11-30 21:07:04 27 4
gpt4 key购买 nike

我有一个数据库有一个名为“详细信息”的节点,它有一些数值子节点。每个子节点都存储姓名、年龄、性别等详细信息。我需要让所有人都看到姓名,而年龄、性别不可见。请帮助我处理 Firebase 规则和 JavaScript 代码。

我已经做了下面给出的规则

{
"rules": {
"register" : {
"details": {
".read": false,
".write": true,
"$userid": {
"name": {
".read": true
},
"age": {
".read": false
},
"gender": {
".read": false
}
}
}
}
}
}

我的数据库是这样的

mainblock
register
details
1
name: "ALPHA"
age: "10"
gender: "Male"
18
name: "BETA"
age: "15"
gender: "Female"
19
name: "GAMMA"
age: "18"
gender: "Male"

而使用的JavaScript代码是

var query = firebase.database().ref("register/details/").orderByKey();
query.once("value").then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var nm = childSnapshot.child("name").val();
console.log(nm)
});
});

但是我有一个错误说

Uncaught (in promise) Error: permission_denied at /register/details: Client doesn't have permission to access the desired data.
at H (firebase-storage.js:3476)
at onComplete (firebase-storage.js:3476)
at Object.onComplete (firebase-storage.js:3476)
at firebase-storage.js:3476
at e.vr (firebase-storage.js:3476)
at t.vr (firebase-storage.js:3476)
at t.cr (firebase-storage.js:3476)
at e.onMessage (firebase-storage.js:3476)
at e.nt (firebase-storage.js:3476)
at e.handleIncomingFrame (firebase-storage.js:3476)

最佳答案

您必须删除第一个读取语句。因为 "cascade in such a way that granting a read or write privilege to a parent node always grants that read/write access to all child nodes" 尝试访问数据库时,只有第一个 ".read" 规则被考虑在内. ".write" 也是如此,只有 ".validate" 不级联,因此树中的所有验证规则都必须适用以允许写入。更多关于 here .

{
"rules": {
"register" : {
"details": {
".write": true,
"$userid": {
"name": {
".read": true
}
}
}
}
}
}

其他 ".read": false 语句是不必要的,因为默认情况下/未设置时权限为 false。

关于javascript - 如何使子节点的某些值 '.read' : true, 而其他值 '.read' : false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46503377/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com