【问题标题】:Why am I not able to write to my firebase database?为什么我无法写入我的 firebase 数据库?
【发布时间】:2018-07-02 22:30:47
【问题描述】:

我正在使用 firebase,但在尝试写入数据库时​​出现权限被拒绝错误。 这是我的规则:

{
  "rules": {
    "foo": {
      ".read": true,
      ".write": true
    }
  }
}

这是写入数据库的代码:

function writeUserData(userId, name, email, imageUrl) {
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });
} 

为什么我无法写入我的数据库?

【问题讨论】:

  • 它有什么错误吗?请使用错误代码更新您的问题
  • 请包含您用于写入数据库的代码。没有它,就不可能肯定地回答这个问题。
  • function writeUserData(userId, name, email, imageUrl) { firebase.database().ref('users/' + userId).set({ username: name, email: email, profile_picture : imageUrl });这是写入数据库的代码。错误说权限被拒绝。
  • 能否给出你的具体规则截图

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


【解决方案1】:

在您当前的规则中,您允许写入数据库的“foo”节点,但您正在尝试写入“用户”。因为您没有为“用户”设置规则,所以 firebase 会使用默认规则,即 false。

如果您希望能够给用户写信,则必须将您的规则更改为:

{
  "rules": {
    "users": {
      ".read": true,
      ".write": true
    }
  }
}

请记住,这允许每个人(甚至是未经身份验证的用户)读取/写入该位置。要获得更安全的选项,您可以对其进行限制,以便用户只能读取/写入自己的数据:

{
  "rules": {
    "users": {
      "$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".write": "$user_id === auth.uid",
        ".read": "$user_id === auth.uid"
      }
    }
  }
}

来自the firebase docs 的示例。

【讨论】:

  • 这是我现在的代码:{ "rules": { "users": { ".read": true, ".write": true } } } 但我仍然收到错误消息:firebase .js:1 未捕获(承诺)错误:PERMISSION_DENIED:权限被拒绝
  • @Kato 这些规则应该适用于您问题中的代码。如果您对此仍有疑问,我建议您更新您的问题以包含您迄今为止尝试过的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
相关资源
最近更新 更多