Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!