0

I have a function like this:

generatedArray.forEach( item => {
            const stocksRef = firebase.database().ref('clients');
            const filteredObject = stocksRef.orderByChild('id').equalTo(item.inn);
            filteredObject.once("value",snapshot => {
                console.log("value", snapshot.val());

                snapshot.forEach((child) => {
                    Object.assign(item, child.val());
                    console.log('kokoko', child.val())
                });

                console.log("After take snapshot from db", generatedArray);

            });

            item.deals.forEach( deal => {
                const stocksRef = firebase.database().ref('list');
                const filteredObject = stocksRef.orderByChild('isin').equalTo(deal.isin);
                filteredObject.once("value",snapshot => {
                    console.log("value", snapshot.val());

                    snapshot.forEach((child) => {
                        Object.assign(deal, child.val());
                        console.log('kokoko', child.val())
                    });

                    console.log("After take snapshot from db", generatedArray);

                });
            })
        })

It adds data from db to an generatedArray. When generatedArray looks like:

[
    {
        'inn': 'string',
        'deals': [{...},{...}]
    },
    {
        'inn': 'string',
        'deals': [{...},{...}]
    }
]

Everything works fine, but if generatedArray had a single object inside like this:

[
    {
        'inn': 'string',
        'deals': [{...}]
    }
]

forEach don't even starts, it shows no any console.logs Why does it happened?

Stas Motorny
  • 141
  • 1
  • 8
  • 1
    It is very hard to believe, can you add a `console.log(item)` inside the the `forEach` callback function. The problem should come from elsewhere – Cedric Cholley Oct 06 '20 at 15:33
  • Make sure you're not running [into the old problem with the lazy console](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) and the data you have is *really* the data you work with. Also, try debugging and stepping through to more accurately see what's happening. – VLAZ Oct 06 '20 at 15:35
  • Added. Now it looks like generatedArray.forEach( item => {console.log("string"); ...some code...}). Situation still the same. First kind of array shows "string" in console.log, the second don't show. – Stas Motorny Oct 06 '20 at 16:30

0 Answers0