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

237
Visualizações
how to return data through function when fetching data from firebase realtime-database in node.js

How can I get this key in DATA and use this key outside of the function?

let DATA = [];

  const database = admin.database();
  let keyref = database.ref("data");

  keyref.once("value", async function (snapshot) {
    let key = await snapshot.val(); // i want this key data out side this function
    DATA.push(key);
    console.log(DATA);
  });

 console.log(DATA); // i want here that inside function key

In short, I want fetched data outside the function

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

0

When fetching RTDB data only once, it is advised to use the get() method. This method is asynchronous, so you need to do something along the following lines:

  async function getRTDBData(ref) {
    const database = admin.database();
    const keyref = database.ref(ref);
    const snapshot = await keyref.get();

    if (snapshot.exists()) {
        return snapshot.val();
    } else {
       return .... // Up to you to adapt here
    }
  }

  getRTDBData("data")
  .then(val => {
      // Do what you want with val
      console.log(val);
  })
about 4 years ago · Juan Pablo Isaza Relatório

0

async function getData() {
  console.log("Refreshing Firebase Credentials");
  const keyref = database.ref("data");
  const snapshot = await keyref.get();

  if (snapshot.exists()) {
    const snapshotVal = snapshot.val();
    console.log(snapshotVal);
    credentials = {
      expires_at: new Date(snapshotVal.expires_at),
      enc_key: snapshotVal.enc_key,
      iv: snapshotVal.iv,
    };
  } else {
    console.log("Firebase Credentials not found");
    process.exit(1);
  }
}

module.exports = {
  getData
};

Use this function where you want to this data

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