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
If you want to add data under userid
then do this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("users").child("userid");
Then the location will be the userid
and any data you add will be under it.
Remove this line above :
myRef = FirebaseDatabase.getInstance().getReferenceFromUrl(" https://my-app-e37d8.firebaseio.com/path/to/data ");
and do something like :
myRef.child("users").child(userId).setValue(YourClass.class);
put your class name instead of YourClass
or if you are storing only string then just do :
myRef.child("users").child(userId).setValue("Hello World");
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.