在实时数据库中添加数据,但添加了错误的子级 [英] Adding Data in the Realtime Database but it adding in the wrong child
问题描述
我正在尝试在userId
下添加新的用户数据,但实际上是将旧数据替换为新数据,并添加了另一个用户"和"user_account_settings".
这是我的实时数据库: 在此处输入图片描述
我的实时数据库必须是这样的:添加新用户数据而不将旧数据替换为新数据,也不要添加用户"和"user_account_settings".
这是我想要的数据库结构示例:在此处输入图像描述 >
这是添加新用户的代码:
public void addNewUser(String email, String username, String bio, String website, String interests, String address, String profile_photo){
User user = new User(userID,1 , email, address, StringManipulation.condenseUsername(username));
myRef.child(mContext.getString(R.string.dbname_users))
.child(userID)
.child("users")
.push()
.setValue(user);
UserAccountSettings settings = new UserAccountSettings(
interests,
username,
0,
0,
0,
"",
username,
website,
bio
);
myRef.child(mContext.getString(R.string.dbname_user_account_settings))
.child(userID)
.child("user_account_settings")
.push()
.setValue(settings);
}
这是我从RegisterActivity.class
调用firebaseMethods.addNewUser
的代码:
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
myRef = FirebaseDatabase.getInstance().getReference();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// 1st check: Make sure the username is not already in use
if (firebaseMethods.checkIfUsernameExists(username, dataSnapshot)){
append = myRef.push().getKey().substring(3,10);
Log.d(TAG, "onDataChange: username already exists. Appending random string to name: " + append);
}
username = username + append;
// add new user to the database
firebaseMethods.addNewUser(email,username,"","","","","");
Toast.makeText(mContext, "Signup successful. Sending verification email.", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
finish();
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
这是我现在的数据结构,Frank van Puffelen: 在此处输入图片描述
您在这些摘录中写错了位置:
.child(userID)
.child("users")
.push()
这会导致路径/$uid/users/-L....
,这不是您想要的.相反,您希望路径为/users/$uid
,这意味着您的代码需要与之匹配:
.child("users")
.child(userID)
因此,您的addNewUser
变为:
public void addNewUser(String email, String username, String bio, String website, String interests, String address, String profile_photo){
User user = new User(userID,1 , email, address, StringManipulation.condenseUsername(username));
myRef.child(mContext.getString(R.string.dbname_users))
.child(userID)
.setValue(user);
UserAccountSettings settings = new UserAccountSettings(
interests, username, 0, 0, 0, "", username, website, bio
);
myRef.child(mContext.getString(R.string.dbname_user_account_settings))
.child(userID)
.setValue(settings);
}
I'm trying to add new user data under the userId
but instead it is replacing the old data into new data and adding another "users" and "user_account_settings".
This is my Realtime Database: enter image description here
My Realtime Database must be like this: adding a new user data without replacing old datas into new datas and also not adding "users" and "user_account_settings".
This the example of database structure I want : enter image description here
This is the code for adding new user:
public void addNewUser(String email, String username, String bio, String website, String interests, String address, String profile_photo){
User user = new User(userID,1 , email, address, StringManipulation.condenseUsername(username));
myRef.child(mContext.getString(R.string.dbname_users))
.child(userID)
.child("users")
.push()
.setValue(user);
UserAccountSettings settings = new UserAccountSettings(
interests,
username,
0,
0,
0,
"",
username,
website,
bio
);
myRef.child(mContext.getString(R.string.dbname_user_account_settings))
.child(userID)
.child("user_account_settings")
.push()
.setValue(settings);
}
This is the code where I call the firebaseMethods.addNewUser
from the RegisterActivity.class
:
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
myRef = FirebaseDatabase.getInstance().getReference();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// 1st check: Make sure the username is not already in use
if (firebaseMethods.checkIfUsernameExists(username, dataSnapshot)){
append = myRef.push().getKey().substring(3,10);
Log.d(TAG, "onDataChange: username already exists. Appending random string to name: " + append);
}
username = username + append;
// add new user to the database
firebaseMethods.addNewUser(email,username,"","","","","");
Toast.makeText(mContext, "Signup successful. Sending verification email.", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
finish();
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
This is my data structure now , Frank van Puffelen: enter image description here
You're writing to the wrong location in these snippets:
.child(userID)
.child("users")
.push()
This leads to a path /$uid/users/-L....
, which is not what you want. You instead want the path to be /users/$uid
, which means your code needs to match that:
.child("users")
.child(userID)
So with that your addNewUser
becomes:
public void addNewUser(String email, String username, String bio, String website, String interests, String address, String profile_photo){
User user = new User(userID,1 , email, address, StringManipulation.condenseUsername(username));
myRef.child(mContext.getString(R.string.dbname_users))
.child(userID)
.setValue(user);
UserAccountSettings settings = new UserAccountSettings(
interests, username, 0, 0, 0, "", username, website, bio
);
myRef.child(mContext.getString(R.string.dbname_user_account_settings))
.child(userID)
.setValue(settings);
}
这篇关于在实时数据库中添加数据,但添加了错误的子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!