【问题标题】:android firebase saving data to realtime databaseandroid firebase 将数据保存到实时数据库
【发布时间】:2017-09-27 08:04:53
【问题描述】:

在 android 应用程序中,我创建了一个通过电话号码进行身份验证的 firebase 用户,它工作正常,然后我更新这个用户添加显示名称,这也很好;最后我想将用户的数据保存到firebase数据库中,所以我写了这个规则:

"users" : {
  "$user_id" : {   // firebase user uid
    ".read"  : "$user_id === auth.uid",
    ".write" : "$user_id === auth.uid",

    ".validate": "newData.hasChildren(['enabled', 'group', 'name'])",

    "enabled" : { ".validate" : "newData.isBoolean()" },
    "group"   : { ".validate" : "newData.isString()" },
    "name"    : { ".validate" : "newData.isString()" },
  }
}

这不起作用,尽管模拟器显示一切正常,但我得到“权限被拒绝”返回(在模拟中,我使用自定义身份验证并将 firebase 设置为具有用户 uid 的提供者)。

用户的班级是:

public class User {
    public boolean enabled;
    public String group;
    public String name;

    public User() { }

    public User(boolean enabled, String group, String name) {
        this.enabled = enabled;
        this.group = group;
        this.name = name;
    }
}

我尝试通过以下方式保存它:

private void saveUserToDB() {
    User newUser = new User(true, "operators", currentUser.getDisplayName());
    mRootRef.child("users").child(currentUser.getUid()).setValue(newUser, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            if (databaseError != null) {
                Log.e("ERROR", "Data could not be saved " + databaseError.getMessage());
            } else {
                Log.e("SUCCESS", "Data saved successfully.");
            }
        }
    });
}

谁能帮我理解为什么会发生这种情况? 谢谢

更新 下面是数据库应该如何的“模式”:

{
    "messages" : {
        "1763409620982" : { // timestamp
            "position" : "position text",
            "sender"   : "sender name",
            "text"     : "message text"
        },
        "1763409734527" : { // timestamp
            "position" : "position text",
            "sender"   : "sender name",
            "text"     : "message text"
        }

        ...
    },

    "users" : {
        "Yi79w0HgA4ZTNAMHadixzoO5PaR2" : { // firebase user uid
            "enabled" : true,
            "group"   : "group 1",
            "name"    : "user display name"
        },
        "kX22c0GgL4ZTNAMHallfgoO4pBN1" : { // firebase user uid
            "enabled" : true,
            "group"   : "group 2",
            "name"    : "user name"
        }

        ...
    }
}

这是完整的规则集:

{
    "rules": {

        "messages" : {
            ".read"    : "root.child('users').child(auth.uid).child('group').val() === 'group 1'",
            ".write"   : "root.child('users').child(auth.uid).child('group').val() === 'group 2'",

            "$tstamp" :{

                ".validate": "newData.hasChildren(['position', 'sender', 'text'])",

                "position"  : { ".validate" : "newData.isString()" },
                "sender"    : { ".validate" : "newData.isString()" },
                "text"      : { ".validate" : "newData.isString()" },
            } 
        },

        "users" : {
            "$user_id" : {
                ".read"  : "$user_id === auth.uid",
                ".write" : "$user_id === auth.uid",

                ".validate": "newData.hasChildren(['enabled', 'group', 'name'])",

                "enabled" : { ".validate" : "newData.isBoolean()" },
                "group"   : { ".validate" : "newData.isString()" },
                "name"    : { ".validate" : "newData.isString()" },
            }
        }
    }
}

至于类user,其定义如上,仅用于方法saveUserToDB

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    尝试在 setValue() 之前添加 push():它将添加包含 User 类对象中所有字段的新子节点:

    mRootRef.child("users").child(currentUser.getUid()).push().setValue(and so on...
    

    【讨论】:

    • 你能分享一下你的数据库结构的快照,还有用户类的实现吗?谢谢!
    • r08y,不确定这是唯一的原因,但 User 类缺少 getter 和 setter。请制作它们并确保它们是公开的。消息也一样。还有一个提示——在你设置硬性规则(尤其是验证)之前——在开始时尝试在没有它们的情况下运行,看看你会得到什么。
    • 好吧,学员,我不认为课程是问题,因为根据firebase documents 他们没问题。至于在开始时制定宽松的规则,即使我将 auth != nulltrue 设置为 read/,我也会得到 "permission denied"写,这很奇怪。好像我正在尝试保存到用户没有授权的不同项目/数据库;我还检查了 google-services.json 似乎没问题。
    • r08y,根据您提供的 firebase 文档:“如果定义它的类具有不带参数的默认构造函数并且具有要分配的属性的公共 getter,则传递自定义 Java 对象。 "但是 - 你的班级缺少吸气剂......
    • 我的错 Cadet,我在阅读那篇文章时非常盲目和粗心。谢谢
    【解决方案2】:

    您可能需要写权限吗?:

    【讨论】:

    • 但是规则里已经规定每个用户可以读写自己的数据,为什么还要增加一个写权限呢?
    • 通常在搜索权限被拒绝原因时,我允许一切,然后一一返回权限。
    • 不可能 nikolai 你知道奇怪的是我得到“权限被拒绝”即使我设置 ".write": "auth.uid != null" 当它被认为是每个人会写
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2021-10-07
    相关资源
    最近更新 更多