Firebase 删除或创建规则

Firebase Delete or a Create rule

在以下 link 的 Firebase 文档中 现有数据与新数据 部分声明以下规则适用于创建或删除数据.

// 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()"

谁能解释一下为什么写入允许删除操作,因为这在我看来是错误的?

在文档的同一部分中指出: newData表示写入的新数据与现有数据的合并结果

问题是,如果有删除操作,那么data会存在,newData也会存在,因为它是合并结果如文档所述。因此,表达式 !data.exists() || !newData.exists() 应该 return false。

如果我遗漏了什么,有人可以告诉我吗?

它在说:

  • 如果没有旧数据(写入空位置)。
  • 或者如果新数据为空(一个删除(清空)的位置)。

能够像这样删除数据很方便,尤其是在进行原子更新时。这意味着 "do these multiple operations, but only if they all succeed"。例如

dbRef.update({
  history: { someNewKey: 'The transaction happened!' },
  pending: { somePendingKey: null }
});

我们在历史记录中写入一个条目 table 并通过使用空写入一次性从待处理的 table 中删除该项目。