Esteban89
Esteban89

Reputation: 691

Push inside foreach not adding to array

I'm trying to run a foreach loop and add values to an array, but it's not working properly. The values in the first foreach get added properly, but none of the values in the second foreach loop get added.

My code is:

categories.forEach(async function (cat) {
        te.push({ params: { slug: [cat.cat_slug] } })
        var articles = await db.query(escape`
                SELECT *
                FROM articles
                WHERE category = ${cat.id}
            `)
        articles.forEach(function (art) {
            te.push({ params: { slug: [cat.cat_slug, art.slug] } })
        })

    })

Any ideas?

Upvotes: 0

Views: 1613

Answers (1)

Esteban89
Esteban89

Reputation: 691

According to this answer Using async/await with a forEach loop Async can't be used inside foreach loops, I had to use a for loop instead and it got fixed.

Upvotes: 1

Related Questions