【问题标题】:Add Data in firebase after doing authentication进行身份验证后在firebase中添加数据
【发布时间】:2017-01-19 22:55:05
【问题描述】:

我的规则是这样的 -

{
"rules": {
    "customer": {
      "$user_id": {
            ".write": false,
            ".read" : "$user_id === auth.uid"
        }
    },
  "franchises": 
  {
        ".read":    true,
        ".write": "root.child('customer').hasChild(newData.child('salon_id').val())"
  }
 }
}

我想在具有 salon_id 的 franchises 中添加数据,并且我想确保该 salon_id 作为 uid 存在于 customer 中。

我遇到了错误

PERMISSION_DENIED:权限被拒绝

使用代码编辑 -

var ref = firebase.database().ref('franchises');
ref.push({
    address: address,
    contact_no: contact_no,
    contact_person_name: contact_person_name,
    name: textinputPlaceName,
    salon_id: salon_id
}); 

数据看起来像这样 -

【问题讨论】:

  • 如果没有看到导致此错误消息的最小代码,就很难提供帮助。
  • 我应该发布什么数据结构?
  • 您包含的错误消息是在您执行某段代码时触发的。您需要在问题中包含触发问题的最少代码。
  • 我认为弗兰克的意思是你应该发布你的代码部分,它实际上试图写一些东西到 firebase,因为这个写操作是被拒绝的。在您的情况下,它将是尝试在特许经营中添加数据的代码。
  • 嗨@FrankvanPuffelen,请检查我的问题,我已经用代码更新了它。

标签: firebase firebase-realtime-database firebase-security


【解决方案1】:

您的规则中缺少一个级别:

"franchises": {
    ".read":    true,
    ".write": "root.child('customer').hasChild(newData.child('salon_id').val())"
}

这适用于您写信至franchises。但是由于你的代码调用ref.push(),你写得更深一层。所以规则应该是:

 "franchises": {
    ".read":    true,
    "$franchiseId": {
      ".write": "root.child('customer').hasChild(newData.child('salon_id').val())"
    }
}

【讨论】:

  • 同样的事情仍然得到 PERMISSION_DENIED
  • 我只更新了规则需要更新代码吗?
  • 我将 $franchiseId 作为 push() 传递,因此它自动生成可能是导致问题的原因。是@frank 吗?
猜你喜欢
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2018-06-20
  • 2017-03-11
  • 2017-03-29
相关资源
最近更新 更多