Here is the layout of my database:
https://i.sstatic.net/yWyUJ.png
I am currently working on a firebase function that iterates through each barbershop and retrieves all their respective Barbers.
In the code snippet below, I have successfully fetched all the barbershop names and stored them in an array, which is then displayed in the console like this:
https://i.sstatic.net/luMVl.png
However, when trying to progress to the next stage of my function, none of the code within "barberShopArray.forEach((key)" seems to be executing, and even "console.log(key)" does not 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;
}));
});