现有数据和新数据究竟意味着什么?

问题描述 投票:2回答:1

请考虑以下规则:

{
  "rules": {
    "foo": {
      ".read": true,
      ".write": "!data.exists() || !newData.exists()",
      ".validate": "newData.isString() && newData.val().length < 100"
    }
  }
}

根据文档,数据表示尝试操作之前的数据,newData表示尝试操作之后的数据。

因此,请考虑foo包含以下字符串。因此data

"foo":{
"Jellybean",
"Kitkat",
"Lollipop",
}

如果进行了添加操作,则newData

"foo":{
"Jellybean",
"Kitkat",
"Lollipop",
"Marshmallow",
}

如果进行了删除操作,则newData

"foo":{
"Jellybean",
"Kitkat",
}

如果进行了更新操作,则newData

"foo":{
"Jellybean",
"Kitkat",
"Nougat",
}

我无法弄清楚".write": "!data.exists() || !newData.exists()"在前两种情况下如何变为真,但在第三种情况下则不然。您能否清楚地了解datanewData的含义以及它如何根据此规则禁止更新请求?

firebase firebase-realtime-database firebase-security
1个回答
0
投票

“数据”始终是数据库中的数据。

In a Update ==>  "newData" is the combinated result after the update. 
In a Insert ==>  "newData" is the data being introduced.

在更新中,数据存在于数据库中,并且newData(组合结果)也存在:

!data.exists() = false 
!newData.exists() = false

在删除操作中,newData为空:

!data.exists() = false 
!newData.exists() = true

如果你正在尝试在/ foo中进行更新,那么方法数据和规则中的newData会返回:

data = "foo":{
 "Jellybean",
 "Kitkat",
 "Lollipop",
 }

newData = "foo":{
 "Jellybean",
 "Kitkat",
 "Lollipop",
 "Marshmallow",
}

newData是更新后元素“foo”在数据库中的方式。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.