问题描述
我有两条路径:
/lotteries
----/uniqueID001
-------/active: true
-------/choicesCutoff
---------/1: 11th August 2020 10:41 AM
---------/2: 11th August 2020 11:41 AM
/tickets
----/uniqueID0234
-------/lottery: uniqueID001
-------/choices
---------/1: 'initial choice'
---------/2: 'initial choice'
我需要创建一个数据库规则,以便人们在截止日期后无法更改选择。
我目前正在使用更新,例如ref('/tickets/uniqueID0234}/choices').update({1: 'new choice'})
并具有客户端逻辑,用于在截止日期后禁用更改。效果很好,但是如果用户更改设备时间,他们将能够更改其票证。
如何编写引用彩票ID和相关的截止日期的规则;在截止后禁用更新?
(每个选择都有不同的截止日期)
解决方法
假设您将截止值存储为时间戳,那将是这样:
{
"rules": {
".read": true,"tickets": {
"$ticketid": {
"choices": {
"$choiceid": {
".write": "root.child('lotteries').child(
data.parent().parent().child('lottery').val()
).child($choiceid).val() > now"
}
}
}
}
}
}
在这里:
-
data.parent().parent().child('lottery')
是当前票证的lottery
。 - 然后,我们建立从数据库
root
到彩票的数据的路径。 - 我们在该彩票的数据下查找当前的
$choiceid
。 - 最后将那个值与服务器上的当前时间进行比较。
代码中可能会有一些小错误,因此请逐步解释其工作方式。