简体   繁体   中英

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

Here is my database Structure:

在此处输入图像描述

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:

在此处输入图像描述

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM