Hello I'm with this error and I'm trying to capture and write about the user's node> email doing so but still giving error, could someone help me? any help is welcome.
the node email already has the comma but when it comes to recovering I still get the error I'm trying to modify the node when receiving with the code below.
my code
addButton.setOnClickListener(new View.OnClickListener() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
#Override
public void onClick(View v) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final String userUid = user.getEmail();
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("users").child(user.getEmail().replace(".",",")).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Map<String,Object> map = new HashMap<>();
map.put("cassinotime",dataSnapshot.child("cassinotime").getValue(String.class));
map.put("cassinoprofit",dataSnapshot.child("cassinoprofit").getValue(String.class));
ref.child("users").child(user.getEmail()).child("cassino").child("cassinotime").setValue(map);
ref.child("users").child(user.getEmail()).child("cassino").child("cassinotime").setValue("50000");
ref.child("users").child(user.getEmail()).child("cassino").child("cassinoprofit").setValue("250");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
register code
private void firebaseAuthWithGoogle(final GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
User user = new User(
);
String photoUrl = null;
if (account.getPhotoUrl() != null) {
user.setPhotoUrl(account.getPhotoUrl().toString());
}
user.setEmail(account.getEmail());
user.setUser(account.getDisplayName());
user.setUid(mAuth.getCurrentUser().getUid());
user.setMoney(money);
user.setCassinotime(cassinotime);
user.setCassinoprofit(cassinoprofit);
FirebaseUtils.getUserRef(account.getEmail().replace(".", ","))
.setValue(user, new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
mFirebaseUser = mAuth.getCurrentUser();
finish();
}
});
} else {
dismissProgressDialog();
}
}
});
Instead of using the email as a parent node for the user's details, it is better to use the userId:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId = user.getUid();
ref.child("users").child(userId).addListenerForSingleValueEvent(/* ... */);
While storing email as a path, I recommend you to encode the email i.e., replace 'dots' from 'commas'. If you want to retrieve, you can decode.
public static String decodeString(String string) {
return string.replace(",", ".");
}
In these three lines of code:
ref.child("users").child(user.getEmail()).child("cassino").child("cassinotime").setValue(map);
ref.child("users").child(user.getEmail()).child("cassino").child("cassinotime").setValue("50000");
ref.child("users").child(user.getEmail()).child("cassino").child("cassinoprofit").setValue("250");
You are still using the original user.getEmail() value. You should use the encoded string like you did in the query. Just do it once and store it in a variable, then reuse the variable.
Even better, just make the reference once and reuse it:
final DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference()
.child("user")
.child(user.getEmail().replace(".",","));
Then in the callback:
ref.child("cassino").child("cassinotime").setValue(map);
ref.child("cassino").child("cassinotime").setValue("50000");
ref.child("cassino").child("cassinoprofit").setValue("250");
Related
firebaseAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
userKey = user.getUid();
mDb.child(userKey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "Name: " + dataSnapshot.child("user_id").getValue());
userID = String.valueOf(dataSnapshot.child("user_id").getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I do not know what is wrong with my code, I want to retrieve value of user id.
To solve this, please use the following code:
firebaseAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
userKey = user.getUid();
mDb.child("Tunanetra").child(userKey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userID = dataSnapshot.child("user_id").getValue(String.class);
Log.d(TAG, "Name: " + userID);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
With this code, you'll be able to print out in the logcat, the id of the users that is authenticated and exists under Tunanetra node. If the user exists under the Orangtua node, then just change Tunanetra with Orangtua.
I think instead of
mDb = mDatabase.getReference();
you should be using
mDb = mDatabase.getReference("Tunanetra");
because that's where the user you're looking for is nested at.
It's simple.
If you want to retrieve user data. Then you have to first select the child node in which you want to look for the user.
You have to Nodes Tunanetra and Orangtua
Else you can try this order by approach.
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
String userKey = user.getUid();
//First Approach
mDb.child("Tunanetra").child(userKey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userID = String.valueOf(dataSnapshot.child("user_id").getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Second Approach
mDb.child("Tunanetra").orderByChild("email").equalTo(user.getEmail()).limitToFirst(1).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getChildrenCount()>0){
for(DataSnapshot data : dataSnapshot.getChildren()){
String userID = String.valueOf(data.child("user_id").getValue());
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I've been trying to retrieve just the child data from here but when i debug it shows null on each object, I'm not sure why, please help.
I've followed some instructions here on Stack overflow but i can't seem to get a solution.
My data on firebase looks like this:
Here is my code:
FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference().child("the-root-db");
DatabaseReference mFirebaseDbReferenceCurrentUser = dbRef.child(mFirebaseUser.getUid());
DatabaseReference userSettingTable = mFirebaseDbReferenceCurrentUser.child("UserSetting");
userSettingTable.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSettingFromDb : dataSnapshot.getChildren()) {
mDbPrice = (String) userSettingFromDb.child("currencyPrice").getValue();
mDbCurrencyId = (String) userSettingFromDb.child("currencyId").getValue();
mDbUserTrackPrice = (String) userSettingFromDb.child("userSettingCurrencyValue").getValue();
break;
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Query Db", databaseError.toString());
}
});
Since you're attaching a ValueListener to a specific node, you will get a DataSnapshot with only that node. This means you don't need to loop over its children.
userSettingTable.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
mDbPrice = dataSnapshot.child("currencyPrice").getValue(String.class);
mDbCurrencyId = dataSnapshot.child("currencyId").getValue(String.class);
mDbUserTrackPrice = dataSnapshot.child("userSettingCurrencyValue").getValue(String.class);
}
I am developing an application in which I have to get the current user details from the firebase UserNode. In my case I want to get firstname and lastname of the current user. I am successfully getting the list of all users but what to do to get the current user details who is logged in.
I use this code to get all users, please guide me what to do in this code to get the current logged in user details
DatabaseReference DataRef;
DataRef = FirebaseDatabase.getInstance().getReference().child("UserNode");
DataRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String acctname = (String)childSnapshot.child("firstname").getValue();
Log.i("name", acctname);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("error", databaseError.getMessage());
}
});
To get a particular user, you need to use in your DatabaseReference his unique identifier. So, you need to change this line:
DataRef = FirebaseDatabase.getInstance().getReference().child("UserNode");
with
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String uid = firebaseUser.getUid();
String uid = firebaseUser.getDisplayName(); //display the entire name
DataRef = FirebaseDatabase.getInstance().getReference().child("UserNode").child(uid);
And please use this code:
DataRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String acctname = childSnapshot.child("firstname").getValue(String.class);
Log.i("name", acctname);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("error", databaseError.getMessage());
}
});
Try this Hope it helps you
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
String userId = firebaseUser.getUid();
String userEmail = firebaseUser.getEmail();
}
}
};
I am using Firebase-UI in my android project with email/password provider. The structure of the database is:
profiles
user-id
age
height
user-id
On signup I want to enter a new node in profiles with default values. The user can edit and save the values later. For an existing user I just want to read the values and display them in the UI. I have tried using ChildEventListener and dataSnapshot.hasChild(uid) to detect if the user already exists but it isn't working. This is the AuthStateListener:
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = mFirebaseAuth.getCurrentUser();
if(user != null)
{
onSignedInInitialize(user);
}
else
{
onSignedOutCleanup();
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().setProviders(providers).build(), SIGN_IN);
}
}
};
This is what I have tried:
private void onSignedInInitialize(final FirebaseUser user) {
userRef = mProfilesDBReference.child(user.getUid());
if(mValueEventListener == null)
{
mValueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue() == null)
{
UserProfile profile = new UserProfile("username", null, 100, 100);
userRef.setValue(profile);
UpdateUI(profile);
}
else
{
UserProfile profile = dataSnapshot.getValue(UserProfile.class);
UpdateUI(profile);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
}
userRef.addValueEventListener(mValueEventListener);
}
Lets say the userid is stored in uid;
You can fetch the height and age like this.
DatabasReference userRef = database.getReference("profiles").child(uid);
userRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue() == null){
//User data doesnt exist
}
else
{
HashMap<String,String> userMap =
(HashMap<String,String>)dataSnapshot.getValue();
String age = userMap.get("age");
String height = userMap.get("height");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Note that i have assumed that you have stored them as Strings , firebase usually converts ints to longs. So if you used ints , convert the value type of hashmap as Long.
A much better way would be to create a POJO class.
I am using Firebase user sign up method with email and password. While creating a new user I also store information such username that later on needs to be displayed in app profile page.
How could I retrieve the username of currently logged in person ?
Thank you in advance
private void registration(){
final String email = Email.getText().toString().toString().trim();
final String password = Password.getText().toString().trim();
final String username = Username.getText().toString().trim();
final String age = Age.getText().toString().trim();
final String userID = userAuth.getCurrentUser().getUid();
if (!TextUtils.isEmpty(email)&& !TextUtils.isEmpty(password)&& !TextUtils.isEmpty(username)&& !TextUtils.isEmpty(age)){
showProgress.setMessage("Registration in progress...");
showProgress.show();
userAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
String user_id = userAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = DatbaseOfUsers.child(user_id);
current_user_db.child("email").setValue(email);
current_user_db.child("username").setValue(username);
current_user_db.child("Age").setValue(age);
current_user_db.child("uID").setValue(userID);
showProgress.dismiss();
//After user is created main screen intent is called
Intent mainpage = new Intent(RegisterActivity.this, MainPageActivity.class);
mainpage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainpage);
}
else if(!task.isSuccessful()){
showProgress.dismiss();
Toast.makeText(RegisterActivity.this,"Error While Register",Toast.LENGTH_LONG).show();
}
}
});
}
else{
showProgress.dismiss();
Toast.makeText(RegisterActivity.this,"Please Enter All Required Fields",Toast.LENGTH_LONG).show();
}
private FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
final FirebaseUser Theuser = mAuth.getCurrentUser();
if (Theuser !=null)
_UID = Theuser.getUid();
You can send request to Users with current user's uid.
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Users").orderByChild(currentUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
In onDataChange returns value you are looking as dataSnapshot. Then cast it to your model, get the want ever you want.