Android Firebase将数据保存到实时数据库 [英] android firebase saving data to realtime database
问题描述
在android应用中,我创建了一个通过电话号码进行身份验证的Firebase用户,它可以正常工作,然后我通过添加显示名称来更新此用户,这也很好;最后,我想将用户数据保存到Firebase数据库中,所以我编写了以下规则:
In android app I create a firebase user authenticated by phone number and it works fine then I update this user adding display name and this is fine too; in the end I want to save user's data into firebase database so I wrote this rule:
"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提供程序),但我仍然返回拒绝权限".
This doesn't work and I get returned "permission denied" altough the simulator shows everything is fine (in the simulation I use custom authentication and set firebase as provider with the user's 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;
}
}
我尝试通过以下方式保存它:
and i try to save it by:
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.");
}
}
});
}
有人可以帮助我理解为什么会这样吗?谢谢
Can anybody kindly help me to understand why this happens? Thanks
更新下面是数据库的模式":
UPDATE below is a "schema" of how the database should be:
{
"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
as to the class user it's defined as above and only used into the method saveUserToDB
推荐答案
尝试在setValue()之前添加push():它将添加新的子节点,其中包含User类对象的所有字段:
Try to add push() before setValue(): It will add new sub-node containing all the fields from object of User class:
mRootRef.child("users").child(currentUser.getUid()).push().setValue(and so on...
这篇关于Android Firebase将数据保存到实时数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!