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

247
Vistas
How do you do a forEach loop on an array containing data retrieved from firebase database JavaScript

Here is my database Structure:

enter image description here

I am attempting to write a firebase function that goes through every barbershop, and retrieves all their Barbers.

In my code below, I have successfully retrieved all the barbershop names, and stored them in an array, which is logged on the console like so:

enter image description here

However when i attempt to move to the next phase of my function, none of the code in "barberShopArray.forEach((key)" executes, and I don't know why. Even "console.log(key)" doesn't work.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

var database = admin.database();

exports.addTimeNod2 = functions.pubsub.schedule('every 24 hours').onRun((context) =>
    {

        var num = 1;
        let barberShopArray = [];
return database.ref('/BarberShops/').once("value").then((snapshot) =>
            {
                snapshot.forEach((childSnapshot) =>
                    {
                        barberShopArray.push(childSnapshot.key);
                    });
                console.log(barberShopArray);
                return barberShopArray;
            }).then(barberShopArray.forEach((key) =>
                {
                    console.log(key);
                    database.ref('/BarberShops/' + key + '/Barbers/Barber1/').once("value").then((snapshot)=>
                        {
                            if(snapshot.exists())
                            {
                                database.ref('metadata/shop' + num +'/').set(key);
                                num++;
                            }
                            return null;
                        }).catch((error)=>
                            {
                                console.log(error);
                                return error;
                            });
                    return null;
                }));
    });
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In my case, I did

const db = firebase.database().ref();

    db.child("orders")
        .get()
        .then((snapshot) => {
            if (snapshot.exists()) {
                const fetchedOrders = [];

                for (let key in snapshot.val()) {
                    fetchedOrders.push({ ...snapshot.val()[key], id: key });
                }
          })

For more reference checkout this Link

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have mentioned that the line console.log(barberShopArray) is working perfectly. After the line console.log(barberShopArray) and before the line barberShopArray.forEach((key) you are using a return statement return barberShopArray. So the part of the function which is after that return statement is not getting executed. Please remove that return statement to resolve the issue. Also the then() method after the return barberShopArray statement is not required. So please modify the code as the following and it should work and successfully update the metadata.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var database = admin.database();
exports.addTimeNod2 = functions.pubsub.schedule('every 24 hours').onRun((context) => {
   var num = 1;
   let barberShopArray = [];
   return database.ref('/BarberShops/').once("value").then((snapshot) => {
       snapshot.forEach((childSnapshot) => {
           barberShopArray.push(childSnapshot.key);
       });
       console.log(barberShopArray);
       barberShopArray.forEach((key) => {
           console.log(key);
           database.ref('/BarberShops/' + key + '/Barbers/Barber1/').once("value").then((snapshot) => {
               if (snapshot.exists()) {
                   database.ref('metadata/shop' + num + '/').set(key);
                   num++;
               }
           }).catch((error) => {
               console.log(error);
               return error;
           });
       });
       return null;
   });
});
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