我有以下 Firebase 数据库规则,但这只允许写入一次数据。之后就什么都没有了。
编辑。我还想确保不会删除任何数据。
{
"rules": {
"$uid": { //userIDTom
".read": "true",
".write": "!data.exists()"
}
}
}
我希望只有在那个 child 不存在的情况下才能写一个 child 。例子,
"userIDTom" : {
"testKey1" : "test1",
"testKey2" : "test2",
"testkey3" : "test3"
}
}
}
以上都将写入“UserIDTom”但在下一个例子中,
"userIDTom" : {
"testKey4" : "test4",
"testKey2" : "update",
"testkey5" : "test5"
}
}
}
在上面的例子中,只写了 testKey4 和 testKey5。 testKey2 将被跳过,因为它已经存在。请注意,即使它的值不同,它仍应被跳过。换句话说,只允许写入新键。
最终结果应该是:
"userIDTom" : {
"testKey1" : "test1",
"testKey2" : "test2",
"testkey3" : "test3",
"testKey4" : "test4",
"testkey5" : "test5"
}
}
}
}
}
最佳答案
In the above example, only testKey4 and testKey5 will be written. testKey2 will be skipped because it already exists
您在这里描述的不是 Firebase 安全规则的工作方式,它们不过滤数据:不是在读取时,也不是在写入时。当您执行写入操作时,整个操作要么被接受,要么被拒绝。
所以如果
userIDTom
您的示例中已经存在,那么任何写入/在它下面的内容都将被拒绝。如果 userIDTom
尚不存在,将允许写入。如果要在写入的任何子 Node 已存在的情况下拒绝写入,请将
.write
规则低一级:{
"rules": {
"$uid": { //userIDTom
".read": "true",
"$property": {
".write": "!data.exists()"
}
}
}
}
关于javascript - 如何设置 Firebase 数据库规则以不允许删除或更新子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63065616/