Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

66
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;
                }));
    });
7 months 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

7 months 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;
   });
});
7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.