Can';不能使用规则写入Firebase数据库

Can';不能使用规则写入Firebase数据库,firebase,firebase-realtime-database,firebase-security,Firebase,Firebase Realtime Database,Firebase Security,我试图创建一个规则,允许一些用户编写,但不是所有用户 我需要所有用户都可以读取“菜单”项,但只有在存储数据中列出的用户才能写入 我的数据结构: { "category" : [ null, "Burger", "Drinks" ], "menu" : [ null, { "available" : true, "category" : "1", "description" : "item1 description",

我试图创建一个规则,允许一些用户编写,但不是所有用户

我需要所有用户都可以读取“菜单”项,但只有在存储数据中列出的用户才能写入

我的数据结构:

{
    "category" : [ null, "Burger", "Drinks" ],
    "menu" : [ null, {
        "available" : true,
        "category" : "1",
        "description" : "item1 description",
        "image" : "chicken_maharaja",
        "name" : "New Chicken Maharaja",
        "price" : 1300,
        "store" : 1
    }, {
                   "available" : true,
                   "category" : "1",
                   "description" : "item2 description",
                   "image" : "big_spicy_chicken_wrap",
                   "name" : "Big Spicy Chicken Wrap",
                   "price" : 120,
                   "store" : 1
               }, {
                   "available" : true,
                   "category" : "2",
                   "description" : "item3 description",
                   "image" : "thumsup",
                   "name" : "Thumsup 100ml",
                   "price" : 40,
                   "store" : 1
               }, {
                   "available" : true,
                   "category" : "2",
                   "description" : "item4 description",
                   "image" : "mccafe_ice_coffee",
                   "name" : "Ice Coffee",
                   "price" : 140,
                   "store" : 1
               }, {
                   "available" : true,
                   "category" : "1",
                   "description" : "item5 description",
                   "image" : "mc_chicken",
                   "name" : "MC Chicken",
                   "price" : 190,
                   "store" : 1
               }, {
                   "available" : true,
                   "category" : "2",
                   "description" : "item6 description",
                   "image" : "Smoothie",
                   "name" : "Smoothie",
                   "price" : 70,
                   "store" : 2
               }, {
                   "available" : true,
                   "category" : "1",
                   "description" : "item8 description",
                   "image" : "salad_wrap",
                   "name" : "Salad Wrap",
                   "price" : 150,
                   "store" : 2
               } ],
    "stores" : [ null, {
        "location" : "Campinas - Taquaral",
        "name" : "Store 1",
        "user" : {
            "pyixsRTw9qdiuESt62YnmEYXQt13" : true
        }
    }, {
                     "location" : "São Paulo - Perdises",
                     "name" : "Store 2",
                     "user" : {
                         "LBNZ8Dwp2rdJtlSh0NC1ApdtbAl2" : true,
                         "TLomOgrd3gbjDdpDAqGiwl0lBhn2" : true
                     }
                 } ],
    "userProfile" : {
        "LBNZ8Dwp2rdJtlSh0NC1ApdtbAl2" : {
            "birthDate" : "1974-02-10",
            "email" : "asd@asd.com",
            "firstName" : "João",
            "lastName" : "Silva"
        },
        "pyixsRTw9qdiuESt62YnmEYXQt13" : {
            "birthDate" : "1974-02-10",
            "email" : "leandro.garcias@gmail.com",
            "firstName" : "Leandro",
            "lastName" : "Garcia"
        }
    }
}
我的规则:

{
"rules": {
    "menu": {
        "$items": {
            ".read":  "true",
            ".write": "root.child('stores').child('1').child(data.child('user').val()).hasChild(auth.uid)"
        }
    },
    "stores": {
        "$store": {
            ".read":  "true",
            ".write": "root.child('stores').child('$store').child(data.child('user').val()).hasChild(auth.uid)"
        }
    }
}
}

读数正常。:-)但是我不会写。

您的
新数据
没有子
用户
,因此检查总是失败。你可能是说:

"43268522": {
  "menu": {
    "$items": {
        ".read":  "true",
        ".write": "root.child('stores').child('1').child('user').hasChild(auth.uid)"
    }
  }
您可能正在寻找以下规则:

".write": "
  root.child('stores')      
      .child(newData.child('store').val())
      .child('user')
      .hasChild(auth.uid)"
因此,它使用新数据中的
store
属性来查找当前用户是否在他们试图修改的存储中

不幸的是,此规则不适用于当前的数据结构,因为
store
的值是一个数字,而store的键是一个字符串:
“1”!==1

最简单的解决方案是将存储存储为字符串,例如:

"store": "1"

你可能想考虑这一点,因为你现在得到了FialBASE数组强制,这是没有帮助的。欲了解更多信息,请参阅我们的博客文章。我建议存储存储时使用其中一种方法,或者只是在它们前面加上前缀,例如

"stores": {
  "store1": {
    ...
  }
}

您的
newData
没有子
user
,因此检查总是失败。你可能是说:

"43268522": {
  "menu": {
    "$items": {
        ".read":  "true",
        ".write": "root.child('stores').child('1').child('user').hasChild(auth.uid)"
    }
  }
您可能正在寻找以下规则:

".write": "
  root.child('stores')      
      .child(newData.child('store').val())
      .child('user')
      .hasChild(auth.uid)"
因此,它使用新数据中的
store
属性来查找当前用户是否在他们试图修改的存储中

不幸的是,此规则不适用于当前的数据结构,因为
store
的值是一个数字,而store的键是一个字符串:
“1”!==1

最简单的解决方案是将存储存储为字符串,例如:

"store": "1"

你可能想考虑这一点,因为你现在得到了FialBASE数组强制,这是没有帮助的。欲了解更多信息,请参阅我们的博客文章。我建议存储存储时使用其中一种方法,或者只是在它们前面加上前缀,例如

"stores": {
  "store1": {
    ...
  }
}

请编辑您的问题以包含无法编写的代码。欢迎使用硬编码路径和值(请参阅)。嗨,弗兰克。我正在Firebase控制台上使用验证工具。我还没有写任何代码。很高兴知道。在这种情况下,请添加模拟器的屏幕截图,显示现有JSON、写入路径、正在写入的数据以及正在使用的身份验证数据。使用此链接:请编辑您的问题,以包含无法写入的代码。欢迎使用硬编码路径和值(请参阅)。嗨,弗兰克。我正在Firebase控制台上使用验证工具。我还没有写任何代码。很高兴知道。在这种情况下,请添加模拟器的屏幕截图,显示现有JSON、写入路径、正在写入的数据以及正在使用的身份验证数据。使用此链接: