Theodore
Theodore

Reputation: 105

How to insert user current user details to another node in Firebase?

I am inserting a data in my Request Node in Firebase database in Android using this,

public void submitRequest(View v) {
Request myUserInsertObj = "Pending";
rootReference.child("Request").child("Pending").child(firebaseuser.getUid()).setValue
(myUserInsertObj);    
}

This is my Request Class.

Public class Request{

public String request_status;

public Request(String request_status){

this.request_status = request_status;
}

Request()
}

I found in firebase documentation that I can use firebaseuser.getDisplayName to get the current logged in user's name. But where will the .getDisplayName get the user's name since I created my own login form and user registration in my app.

Question 2: If I do this, is this possible? Because I want to put a name in requesting guest node so that when I retrieve it in my HTML web admin panel the data will be easier to read.

  rootReference.child("Request")
        .child("Pending")
        .child(firebaseuser.getUid())
        .setValue(myUserInsertObj + firebaseuser.getDisplayName);

If so what should I add in my Request Class?

Question 3. How do I add timestamp I know timestamp is very important in data insertion on every system.

Upvotes: 2

Views: 300

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 139039

I found in firebase documentation that I can use firebaseuser.getDisplayName to get the current logged in user's name.

Yes, that correct. Calling getDisplayName() on a FirebaseUser object:

Returns the main display name of this user from the Firebase project's user database.

Regarding the second part of your question:

But where will the .getDisplayName get the user's name since I created my own log in form and user registration in my app.

As explained above, getDisplayName() is getting you the name that is coming from the authentication process. If you want to get the user name from your custom user object then you should first get the user object from the database and use it where it is needed.

Because I want to put a name in the request node so that when I retrieve it in my HTML web admin panel the data will be easier to read.

If you want to add the name in your node, you should pass the display name to the child() method and not to the setValue(). Your code should look like this:

rootReference.child("Request")
    .child("Pending")
    .child(firebaseuser.getUid() + "_" + firebaseuser.getDisplayName())
    .setValue(myUserInsertObj);

This code will generate a child that might look like this:

Firebase-root
   |
   --- Request
        |
        --- Pending
              |
              --- SxbVg0...uobvk1_Theodore
                    |
                    --- //user details

DatabaseReference class has 4 overloaded setValue() methods but none of this methods allow you to pass an object along with a String as arguments.

Question 3. How do I add timestamp I know timestamp is very important in data insertion on every system.

This is how you add and get the timestamp that you were talking about.

Upvotes: 1

Related Questions