Reputation: 573
I'm trying to check it in Database with an addListenerForSingleValueEvent method, but when I debug the code the listener is skipped, this is my code of the listener.
mTheReference = FirebaseDatabase.getInstance().getReference();
mUsersReference = mTheReference.child("Users");
mUsersReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
User post = postSnapshot.getValue(User.class);
String username = post.getUsername();
if (username.equals(mUserView.getText().toString())) {
alreadyRegisteredAccount++;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
if (alreadyRegisteredAccount != 0) {
mUserView.setError("Usuario ya registrado, intenta de nuevo.");
} else {
User user = new User();
user.setUsername(mUserView.getText().toString());
user.setPassword(mPasswordView.getText().toString());
mUsersReference.push().setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getApplicationContext(), "YAAAY!", Toast.LENGTH_LONG).show();
}
});
}
I don't know why its skipped, I need something that let me check if a user exists once the user tries to register a new username.This is the data tree:
{
users:
{
-KjGdQAZEfJ-qR5pM0Ko:
{
username : fmigg
password : broxton1
}
-KjGq7AghI9ftiX6Ih-q:
{
username : aston
password : creative123
}
}
}
Upvotes: 0
Views: 3537
Reputation: 138824
Your DatabaseReference
is wrong. Please change this line:
mUsersReference = mTheReference.child("Users");
with
mUsersReference = mTheReference.child("Users").child(userId);
In which userId
is the unique id generated by the push()
method.
Hope it helps.
Upvotes: 2