每次用户在Android中注册时,都会在firebase数据库中创建新的用户条目

时间:2018-05-24 14:14:34

标签: java android firebase firebase-realtime-database

我目前正在开发一个测验应用,用户通过电子邮件注册并通过firebase注册。

注册后,他将传递一个昵称,用于高分。

所以基本上我希望应用程序每次用户注册时都会添加这些用户信息,如附图所示。

enter image description here

为了达到这个目的,我使用了这段代码:

UISlider

现在的问题是,我做错了什么? 在firebase的身份验证窗口中创建了一个用户,但数据库没有更新。

我是否正确定位了数据库?你如何决定要定位哪个数据库?

这些是我的firebase安全规则: {  "规则":{    " .read":false,    " .write":false  } }

关于数据库,如果我做对了,就不会发生任何事情。

我没有得到任何错误消息。

1 个答案:

答案 0 :(得分:0)

首先,我会向add a completion callback建议您的代码,以便了解您对Firebase的写入何时以及是否失败/成功。

在您的情况下,您会收到权限被拒绝错误,因为您的安全规则设置为false - >没有人有权读/写您的数据库。相反,您可以授予每个用户写入自己数据的权限,如下所示:

{
  "rules": {
    "userlist": {
      "$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"
      }
    }
  }
}

(有关详细信息,请参阅the documentation

要使上述工作正常,您还必须更改用于写入数据库的代码。目前您正在使用push()。这个generates a random id并且你不需要(想要)这里。相反,您可以使用setValue()

<击> myref.child(&#34;用户列表&#34)。推()的setValue(用户ID);

myref.child("userlist").child(UserId).child("email").setValue(EmailEntry);
myref.child("userlist").child(UserId).child("highscore").setValue(0);

您还可以查看my answer here以获取有关此主题的更多信息。