skip to Main Content

i have this database:

users:                   get(ref(db, `users/${db_uname}/game`)).then((snapshot) => {
  000:                     if (!(snapshot.exists())) {
    game:                    set(ref(db, `users/${db_uname}/game`), {
      cord_x: 0                cord_x: 0,
      cord_y: 0                cord_y: 0
      online: true           });
    password: "000"         }
                            update(ref(db, `users/${db_uname}/game`), {
                              online: true
                            });
                            var con = true;
                            window.db_con = con;
                          });

And i need to get username, game/cord_x, game/cord_y from all users with game/online == true, to call a function with them later. Please help! 🙁

function getUsers() {
  get(child(db, `users/`)).then((snapshot) => {
      (( DONT KNOW HOW ))
      create_hero(name, x, y);
    })
  });
}

2

Answers


  1. Chosen as BEST ANSWER

    Based on Frank van Puffelen's answer i made what i needed, if someone ever needs needs it, here it is:

    function getUsers() {
      get(ref(db, 'users')).then((snap) => {
        snap.forEach((userSnap) => {
          if (userSnap.child("game").exists) {
            if (userSnap.child("game/online").val()) {
              createHero(userSnap.key, userSnap.child("game/cord_x").val(), userSnap.child("game/cord_y").val());
            } else {
              removeHero(userSnap.key);
            }
          }
        });
      });
    }
    

  2. To get the db_uname, cord_x and cord_y for all nodes from your data structure, you can do:

    get(child(db, `users/`)).then((snapshot) => {
        snapshot.forEach((userSnapshot) => {                    // 👈 loop over all users
          console.log(userSnapshot.key);                        // "000"
          console.log(userSnapshot.child("game/cord_x").val()); // 0
          console.log(userSnapshot.child("game/cord_y").val()); // 1
        });
      })
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search