【问题标题】:Unique values in synchronized array同步数组中的唯一值
【发布时间】:2016-04-30 20:26:39
【问题描述】:

我为每个用户存储了一个包含某种 id 的同步数组。 在网络应用程序中,我有两个函数:addId(id)removeId(id),我将它们翻译成正确的 synchronizedArray.$add(id)synchronizedArray.$remove(getRefForId(id))

在我的存储中: root > users > $uid > data > ids: synchronizedArray 定义为:

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

有什么方法可以验证这个同步数组,使用户不能多次添加相同的 id(换句话说:用户数据/id 中的值是唯一的)?

【问题讨论】:

    标签: firebase angularfire firebase-security


    【解决方案1】:

    使用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

    【讨论】:

    • 我不希望键是唯一的。我需要独特的价值观。
    • 没有任何内置功能可确保值的唯一性,因此您必须更改/扩充您的数据结构以将这些值转换为键。可能类似于indexes documented here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2016-03-13
    • 2012-03-27
    • 2017-01-31
    相关资源
    最近更新 更多