2016-04-30 80 views
0

我为每个用户存储某种类型的ID的同步数组。 在一个网络应用程序中,我有两个功能:addId(id)removeId(id),我将它们翻译成适当的​​和synchronizedArray.$remove(getRefForId(id))同步数组中的唯一值

在存储我有: 根>用户> $ UID>数据> IDS:synchronizedArray定义为:

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

是否有任何方式来验证的方式这个同步阵列用户不能添加相同的ID多次(换句话说:用户的数据/ ID中的值是唯一的)?

回答

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()你的IDS进行统计保证是唯一的了。见this article about Firebase's so-called push ids

+1

我不希望密钥是唯一的。我需要价值观才是唯一的。 – Ari

+0

没有内置任何内容来确保唯一值,因此您必须更改/扩充数据结构以将这些值转换为密钥。可能类似于[记录在这里的索引](https://www.firebase.com/docs/web/guide/structuring-data.html#section-indices)。 –