Firebase使用newData进行验证 [英] Firebase validate with newData

查看:161
本文介绍了Firebase使用newData进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一个很好的例子来说明如何构建数据,如Kato在本文中所示:


我没有任何运气验证工作正常。



数据结构是:

  accepted_invites 
game1
descfun game

invites
game1
uuidAAA true
uuidBBB true

以下是一个屏幕截图:
Firebase数据

如果我尝试写下面的内容:

  ref 。.child( accepted_invites)子( 的Game1)子( userTwo)子( uuidBBB)的setValue(真)。 

使用此规则在accepted_invites中输入:

 。validate:root.child('invites /'+ $ game_id +'/ uuidBBB')。exists()


$ b

():root.child('invites /'+ $ game_id +'/'+ newData.val())。exists()

我尝试使用模拟器,但我得到
类型错误:+只对数字和字符串进行操作。



完成我有他们的规则︰

  {
rules:{
.write:true ,
.read:true,
accepted_invites:{
$ game_id:{
$ user_id:{
//这个验证规则失败
//\".validate:root.child('invites /'+ $ game_id +'/'+ newData.val())exists()
//这个工程
.validate:root.child('invites /'+ $ game_id +'/ uuidBBB')。exists()
}
}
}
}
}


解决方案 div newData 关键字是一个涉及所有传入数据的特殊属性,但是它在路径表达式中是不允许的,因为它可能不是是一个字符串。也就是说,它可能是一个对象。



如果您有兴趣在路径中使用这些数据的某些部分,我建议在其中添加另一个验证规则更深的路径,例如:

$ p $ {
rules:{
.write:
.read:true,
accepted_invites:{
$ game_id:{
$ accepting_user_id:{
$ sending_user_id :{
.validate:root.child('invites')。child($ game_id).child($ sending_user_id).exists()
}
}
}
}
}
}


I'm working thru the excellent example on how to structure data as shown by Kato in this post: Firebase user account security with firebaseSimpleLogin

I'm not having any luck getting validate to work properly.

The data structure is :

accepted_invites
    game1
       desc  "fun game"

 invites
     game1
       uuidAAA    true
       uuidBBB    true

here's a screen shot: Firebase data

If I try and write the following

   ref.child("accepted_invites").child("game1").child("userTwo").child("uuidBBB").setValue(true);

it will make an entry in accepted_invites with this rule :

".validate": "root.child('invites/'+$game_id+'/uuidBBB').exists()"

but not

".validate": "root.child('invites/'+$game_id+'/'+newData.val()).exists()"

I tried using the simulator but I'm getting Type Error: + only operates on numbers and strings.

Here's the complete rules as I have them:

{
  "rules": {   
    ".write" : true,
    ".read" : true,
  "accepted_invites": {
    "$game_id": {
      "$user_id": {
        //This validate rule fails  
        //".validate": "root.child('invites/'+$game_id+'/'+newData.val()).exists()"
        //This one works
        ".validate": "root.child('invites/'+$game_id+'/uuidBBB').exists()"
      }
    }
  }
}
}

解决方案

The newData keyword is a special attribute referring to all incoming data, but it is not permitted in path expressions because it may not be a string. i.e., it could very well be an object.

If you're interested in using some portion of that data within the path, I would recommend just including another validation rule at a deeper path, such as:

{
  "rules": {   
    ".write" : true,
    ".read" : true,
    "accepted_invites": {
      "$game_id": {
        "$accepting_user_id": {
          "$sending_user_id": {
            ".validate": "root.child('invites').child($game_id).child($sending_user_id).exists()"
          }
        }
      }
    }
  }
}

这篇关于Firebase使用newData进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆