简体   繁体   中英

JavaScript: alerting a specific value on my real-time database

I've been trying to achieve the following, but I couldn't

and here is my snapshot data: 屏幕

If I wanted to alert ada's first name, what code should i use?

I used this, it returns undefined:

    if (user) {
    // User is signed in.
      alert('user signed')

      // Get a reference to the database service
var database = firebase.database();

      var query = firebase.database().ref("users");
query.once("value")
  .then(function(snapshot) {


    snapshot.forEach(function(childSnapshot) {
      // key will be "ada" the first time and "alan" the second time
      var key = childSnapshot.key.val;
      // childData will be the actual contents of the child
      var childData = childSnapshot.val();

        alert(key);
  });

but it didn't work, any suggestions?

error image: SCE

note: completely new to JavaScript

in the docs , it is

// key will be "ada" the first time and "alan" the second time
var key = childSnapshot.key;

not var key = childSnapshot.key.val;

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