问题描述
在执行更新操作时,在Firebase安全规则中使用newData时遇到问题.
I'm having issues using newData within Firebase security rules when doing a update operation.
这是我的数据结构:
这是我的规则:
{
"rules": {
"videos": {
"$videoId": {
"data": {
"content": {
"srcURL": {
".write": "root.child('videos/' + $videoId + '/tokens/' + newData.child('/videos/-1vidid/requestToken').val() + '/read' ).exists()"
},
},
"meta": {
"status": {
".write": "root.child('videos/' + $videoId + '/tokens/testTok/read').exists()"
}
}
},
"requestToken": {
".write": true,
}
}
}
}
}
这是我要通过更新写入的数据:
Here is data I'm trying to write via update:
{
"videos/-1vidid/data/meta/status": "uploaded",
"videos/-1vidid/data/content/srcURL": "https",
"videos/-1vidid/requestToken": "testTok"
}
这是我尝试使用newData时失败的规则;
And here are my rules failing when I try to use newData;
请注意,当我在令牌("testTok")中进行硬编码时,它可以工作,但是当我尝试使用newData时,它将无法清除.
Notice it's working when I hardcode in the token ("testTok"), but when I try and use newData it won't clear.
当我从newData子级中删除"videos"和$ videoId(-1vidid")时,还有更多尝试:
Here are more attempts when I get rid of the "videos" and $videoId ("-1vidid) from the newData childs:
任何想法为何newData似乎都无法进行更新?
Any idea why newData does not appear to be working on updates?
推荐答案
您的相关规则:
root.child('videos/' + $videoId + '/tokens/' + newData.child('/videos/-1vidid/requestToken').val() + '/read' ).exists()"
newData
变量引用当前位置的新数据.在您定义规则的位置下没有子/videos
.
The newData
variable refers to the new data at the current location. There is no child /videos
under the location where you've defined the rule.
我假设您想从根开始阅读.在这种情况下,您可以使用root
(在根目录中提供现有数据),也可以使用newData.parent().parent()...
(对于需要进行的多个级别重复该操作)来获取更新的数据.
I assume you want to start reading from the root. In that case you can either use root
, which gives you the existing data at the root, or use newData.parent().parent()...
(repeated for however many levels up you need to go) for getting the updated data.
这篇关于在Firebase安全规则中的更新上使用newData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!