同步数组中的唯一值

时间:2016-04-30 20:26:39

标签: firebase angularfire firebase-security

我为每个用户存储某种ID的同步数组。 在网络应用中,我有两个功能:addId(id)removeId(id),我将它们翻译为正确的synchronizedArray.$add(id)synchronizedArray.$remove(getRefForId(id))

在存储中我有: root>用户> $ uid>数据> ID:synchronizedArray定义为:

var ref = new Firebase('https://myapp/firebaseio.com/users/' + userUid + '/data/ids');
var synchronizedArray = $firebaseArray(ref);

有没有办法以用户无法多次添加相同ID的方式验证此同步数组(换句话说:以用户数据/ ID中的值是唯一的方式)?

1 个答案:

答案 0 :(得分:0)

使用Firebase's Security Rules language可以轻松确保数据不会被覆盖。从该指南:

  

为了说明,只要数据在给定路径上不存在,而不是对数据进行更改,请考虑允许我们创建新记录或删除现有记录的规则:

// we can write as long as old data or new data does not exist
// in other words, if this is a delete or a create, but not an update
".write": "!data.exists() || !newData.exists()"

但是如果您使用的是$add(),那么您的ID在统计上已经保证是唯一的。请参阅this article about Firebase's so-called push ids