Firebase 实时数据库 - Javascript 查询仅检索值,不包括键或 "name"

Firebase Realtime database - Javascript query to retrieve values only, not including the key or "name"

我目前正在使用 firebase 实时数据库并按照以下结构写入数据库(以 JSON 格式显示)。第一行仅供参考,第二行是该位置数据的实际示例。数据库中有几百条记录是“push_token”的子记录。

{
  "users" : {
    "push_token" : {
      "key or name" : "value",
      "-Maer1EuNpR24LdhxmIl" : "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
    }
  }
}

下面的 Javascript 查询允许我从数据库中获取数据,但是,我想 console.log 只是值,而不包括键或“名称”。也就是说,我只想包含冒号右侧的数据(ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]”),而不是左侧的自动分配的随机密钥。

import { getDatabase, ref, child, get } from "https://www.gstatic.com/firebasejs/9.6.4/firebase-database.js";
const db = getDatabase();
//----References----//
    var submitButton = document.getElementById("SubmitButton");

//----query database for ExpononentPushToken Values----//

  function SelectData(){
    const dbRef = ref(db);

    get(child(dbRef, `users/${"push_token"}`))
    .then((snapshot) => {
        if(snapshot.exists()) {
            console.log(snapshot.val());
        }
        else {
            console.log("No data found");
        }
    })
        .catch((error)=>{
        console.error(error);
    });
  }

//----Assign Events to Buttons----//
  submitButton.addEventListener('click', SelectData);
</script>

有没有办法以这种方式表达数据检索?也就是,除了key,只获取这个db结构的值?

我很感激我能得到的任何帮助。我四处搜索,但找不到任何简单的解决方案。

要仅记录 users/push_token 下的属性值,您可以这样做:

get(child(dbRef, "users/push_token"))
.then((snapshot) => {
    snapshot.forEach((child) => {
        console.log(child.val());
    })
})
.catch((error)=>{
    console.error(error);
});

这里新增的snapshot.forEach()遍历快照下的所有属性