簡體   English   中英

獲取所有具有特殊行值的用戶表值,firebase web 數據庫

[英]get all users' table values with special row value, firebase web 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;
                          });

我需要從所有具有游戲/在線 == true 的用戶那里獲取username, game/cord_x, game/cord_y ,以便稍后與他們一起調用函數。 請幫忙: :(

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

要從數據結構中獲取所有節點的 db_uname、cord_x 和 cord_y,您可以執行以下操作:

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

根據Frank van Puffelen 的回答,我做了我需要的,如果有人需要它,這里是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM