Centell
Centell

Reputation: 409

How to get user infomation with uid in firebase?

What I want to make

A system in which users send messages to administrators.

Progress

If the user leaves a message, the administrator wants to see a list of them.

In the database,

messages {
    uid1 {
        key1 {
            name : "user name"
            text : "text that user remained"            
            created_at : "time that message created"
        }
        key2 {
            …
        }
        …
    }

    uid2 {
        …
    }
}

I store the information as above. now be able to view this information as a list on the admin page..(and admin click on the list to see the message.)

However, I have succeeded in loading each uid, but I do not know how to use `uid 'to fetch the user's information.

Here is the output(now):

<div>uid1</div>
<div>uid2</div>

Here is the code:

function getListItem() {
  var messagesRef = database.ref('personalchats') //personalchats 에 있는 uid로 접근
  messagesRef.on('child_added', setListItem);
  messagesRef.on('child_changed', setListItem);

}

function setListItem(data) {
  var key = data.key;

  var html = '<div class="listitem"> ' + key + '</div>;

   $(".collection").append(html);
}

I want this result to be:

<div>userName1 : current message</div>
<div>userName2 : current message</div>
...

What can I do for it?

Thank you.

Upvotes: 0

Views: 53

Answers (1)

reflexgravity
reflexgravity

Reputation: 970

convert the data using data() and then extract it using the value names given in the database.

   function getListItem() {
      var messagesRef = database.ref('personalchats') //personalchats 
      messagesRef.on('child_added', setListItem);
      messagesRef.on('child_changed', setListItem);
    }

   function setListItem(data) {
      var key = data.key;
      // create a variable to store the data
      var dataVal=data.val();
      var msgtext=dataVal.text;

      var html = '<div class="listitem"> ' + key + ': '+ msgtext</div>;
       $(".collection").append(html);
    }

Upvotes: 1

Related Questions