简体   繁体   中英

How to get user infomation with uid in firebase?

What I want to make

A system in which users send messages to administrators.

Progress

  • get uid (success)
  • get user info (fail)

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.

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);
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM