是否可以并建议在Firebase服务器上定义需要考虑时间戳计算的写入规则?
示例
场景:用户正在尝试向现有线程添加新的评论。
写入规则:如果当前时间介于Thread.openAtTimestamp和Thread.closesAtTimestamp之间,则只能将注释添加到现有线程。
例如,使用momentjs可以很容易地解决这个问题。但是我想在Firebase规则中没有提供时刻的libjs?
答案 0 :(得分:3)
假设你有这个数据结构:
threads
$threadid
openAtTimestamp: 1453820367233
closesAtTimestamp: 1454425139712
comments
$threadid
$commentid
author: "Ismar Slomic"
timestamp: 1454425139711
然后,只有当timestamp
位于该帖子的openAtTimestamp
和closesAtTimestamp
之间时,您才能在帖子中发表评论。
{
"rules": {
"comments: {
"$threadid": {
"$commentid": {
"timestamp: {
".validate": "
newData.val() > root.child('threads').child($threadid).child('openAtTimestamp') &&
newData.val() < root.child('threads').child($threadid).child('closesAtTimestamp')
}
}
}
}
}
这只是一个粗略的大纲,可以帮助您入门。 Firebase documentation on security rules有更多信息。