Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
trying to return a variable with values ​from the bank and returns undefined

I'm using firebase realtime database. I'm trying to get the data from the database and return it.

I need to return because I'm calling a function that should do this, get the data from the database and return. But in the function I'm calling this 'getData()' function, the value comes as 'undefined'.

Do I have to use asynchronous functions?? Or have another method. someone explain to me please

import { construct_bd } from "../../database.js";

function blog_posts() {
   let section = document.getElementById("blog_posts_container");

   function show() {
      let data = construct_bd.getData("/posts");
    
      console.log(data)
   }
   setTimeout(() => {
      show()
   }, 200);
   ...
}

database.js

...
// Variables
let database = firebase.database();
...
getData(reference){
    let data;
    database.ref(reference).once("value")
    .then(snapshot => data = snapshot.val())
   .catch(error => console.log(error))
   .finally(() => {return data})
},
...
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If the getData function returns a promise, then use await in your calling function. For example

async function show() {
  let data = await construct_bd.getData("/posts");
  console.log(data)
}

If getData is not a promise you can change it accordingly.

getData(reference){
    return new Promise((resolve,reject) => {
    database.ref(reference).once("value").
      then(snapshot => 
        data =  snapshot.val();
         resolve(data);
       ).catch(
         error => reject(error)
       ).finally(() => 
         reject(data);
       )
    
     });
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda