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

196
Visualizações
how to get all data in correct way firebase-real-time-database JavaScript

I am using node.js and I am getting data from firebase real-time database. The problem is I am getting data something like this :

for data getting code! JS

import firebaseApp from '../config.js';
import { getDatabase, ref, onValue } from "firebase/database";


const userRef = ref(database, "Users");
onValue(userRef, (snapshot) => {

if (snapshot.exists) {
  const data = snapshot.val();
  console.log(data); // data printed to console
}

}, {
  onlyOnce: true
});

Console Output

{
  
 "random-user-id-1": {
    "name": "Jhon Doe",
    "profile": "profilelink",
    "email": "example@email.com"
 },
 
 "random-user-id-2": {
    "name": "Cr7",
    "profile": "profilelink",
    "email": "example@email.com"
 },

 // and more...

}

I want to display this data as an array of objects. Example of expected output

[

    {
    "name": "Jhon Doe",
    "profile": "profilelink",
    "email": "example@email.com"
    },
    
    {
    "name": "Cr7",
    "profile": "profilelink",
    "email": "example@email.com"
    }

    // and more........ ^_~

]
Any help will be always appreciated! and feel free to ask any doubts related to my question or problem!

thank you :)

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

0

seems like you need only the values from your dict, you can transform your data this way:

const lst = {
  
 "random-user-id-1": {
    "name": "Jhon Doe",
    "profile": "profilelink",
    "email": "example@email.com"
 },
 
 "random-user-id-2": {
    "name": "Cr7",
    "profile": "profilelink",
    "email": "example@email.com"
 },

}

const expectedFormatRes = Object.values(lst);


console.log(expectedFormatRes);

about 4 years ago · Juan Pablo Isaza Relatório

0

An alternative to Gil's answer would be to use Firebase's built-in. forEach operation:

if (snapshot.exists) {
  let values = [];
  snapshot.forEach((child) => {
    value.push(child.val());
  })
  console.log(values);
}

While longer, this has the advantage that it maintains the order in which the database returned the data, which becomes relevant when you specify an orderBy... clause on your query..

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