Firebase security-newData()作为hasChildren()表达式的参数

Firebase security-newData()作为hasChildren()表达式的参数,firebase,firebase-security,Firebase,Firebase Security,我希望仅当用户拥有令牌(邀请)时,才允许用户创建其帐户。 在我的firebase Security中,我有: { "rules": { ".read": false, ".write": false, "users": { // allow to write if in the invitations there is child equal to token from newData() ".write":"root.child('invitations').has

我希望仅当用户拥有令牌(邀请)时,才允许用户创建其帐户。 在我的firebase Security中,我有:

{
"rules": {
  ".read": false,
  ".write": false,
  "users": {
    // allow to write if in the invitations there is child equal to token from newData()
    ".write":"root.child('invitations').hasChild(newData.child('token').val())",  
  },
 "invitations":{
   "$invitation":{
     ".read": "true"
   }
  }
}
}

如果在邀请中有等于newData()中token的子项,则我希望允许在注释中写入

从模拟器:

Attempt to write {"token":"evl6yky3vi0pmn29","name":"John"} to /users/99 with auth=null
/:.write: "false"
    => false
/users:.write: "root.child('invitations').hasChild(newData.child('token').val())"
6:52: hasChild() expects a string argument.
    => false
/users/99:<no rules>
尝试将{“token”:“evl6yky3vi0pmn29”,“name”:“John”}写入/users/99,auth=null
/:.写:“假”
=>错误
/用户:。写入:“root.child('investments').hasChild(newData.child('token').val())”
6:52:hasChild()需要一个字符串参数。
=>错误
/用户/99:

我该怎么做呢?

你几乎做到了完美。这里唯一的缺陷是,您试图写入users/99,但您将规则设置为users/

你的意思大概是:

"users": {
   "$user_id": {
      // allow to write if in the invitations there is child equal to token from newData()
      ".write":"root.child('invitations').hasChild(newData.child('token').val())",  
   }
},

你几乎做到了完美。这里唯一的缺陷是,您试图写入users/99,但您将规则设置为users/

你的意思大概是:

"users": {
   "$user_id": {
      // allow to write if in the invitations there is child equal to token from newData()
      ".write":"root.child('invitations').hasChild(newData.child('token').val())",  
   }
},

我用这个解决方案来解决我的问题;但是,规则允许我们使用多个通配符吗?val()函数是否与exists()函数相同?否,它返回值。Exists返回一个布尔值。在本例中,我们使用该值作为键,因此exists根本不起作用。API引用是您在这里的朋友。到目前为止,错误地将val解释为有效:)。非常感谢。因此,'.hasChild(newData.child('token').val())与;'child(newData.child('token').val()).exists()'我已经用这个解决方案解决了我的问题;但是,规则允许我们使用多个通配符吗?val()函数是否与exists()函数相同?否,它返回值。Exists返回一个布尔值。在本例中,我们使用该值作为键,因此exists根本不起作用。API引用是您在这里的朋友。到目前为止,错误地将val解释为有效:)。非常感谢。因此,'.hasChild(newData.child('token').val())与;'child(newData.child('token').val()).exists()