Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
Firebase Realtime database - Javascript query to retrieve values only, not including the key or "name"

I am currently using firebase realtime db and writing to the db in the structure below (shown in JSON format). The first row is just for reference, second row is actual example of the data in place in this location. There are a few hundred records that are child to "push_token" in the db.

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

The below Javascript query is allowing me to get the data from the database, however, I'd like to console.log JUST the value, and not include the key or "name". That is, i'd like to only include the data to the right of the colon (the ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]") and not the auto-assigned random key on the left.

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>

Is there anyway to articulate data retrieval in this manner? That is, to only get the value, apart from the key, with this db structure?

I appreciate any help I can get on this. I searched around, but could not find any simple solutions.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To only log the values of the properties under users/push_token, you can do:

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

The new snapshot.forEach() in here iterates over all properties under the snapshot.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda