【问题标题】:Android Firebase cant add child and setvalueAndroid Firebase 无法添加子元素和设置值
【发布时间】:2017-03-10 20:08:33
【问题描述】:

我按照文档中的方法做了,但是我发送的值也没有在数据库中注册。 我可以在登录前添加到数据库,但我不能在登录后添加 我的代码是这样的

    edit_name=(EditText)findViewById(R.id.edit_namepro);
    edit_adress=(EditText)findViewById(R.id.edit_adresspro);
    String name = edit_name.getText().toString().trim();
    String adress = edit_adress.getText().toString().trim();
    User userInfo = new User(name,adress);

    FirebaseUser user = firebaseAuth.getCurrentUser();

        db.child("Users").child(user.getUid()).setValue(userInfo.name);
        Toast.makeText(ProfileActivity.this, "Saved", Toast.LENGTH_SHORT).show();




    edit_adress.setText("");
    edit_name.setText("");

我的规则是这样的

 {
"rules": {
".read": "auth ==null",
".write": "auth == null"
}
 }

感谢大家的回答

【问题讨论】:

  • 快速猜测是当您写入数据库时​​您的用户尚未登录。要检测到这一点,请参阅stackoverflow.com/documentation/firebase/5548/…
  • 感谢您的回答,现在我尝试在 MainActivity 中写入数据库并且它发生了。但是我登录后无法在数据库中注册。
  • 这听起来像是一个单独的问题。

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


【解决方案1】:

您的规则规定只有在用户登录时才允许读/写操作。这是故意的吗?

抱歉,请尝试将这两个规则更改为:

auth != null

【讨论】:

  • 感谢您的回答。不,我该如何解决?
【解决方案2】:

把你的规则改成

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

并确保用户在将任何内容放入 firebase 之前已登录。这有助于跟踪在 firebase 上输入的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多