0

Suppose you have posts and categories data from a relation database:

// category_id on categories.id
const posts = [
  { id: '1', title: 'dream pc', category_id: '1' },
  { id: '2', title: 'my dream house design', category_id: '1' },
  { id: '3', title: 'bing chillin', category_id: '2' },
]

const categories = [
  { id: '1', name: 'wishlist' }, 
  { id: '2', name: 'favorites' }
]

This is my route format: /categories/[category]/[page]

With directory structure of pages/categories/[category]/[page].js.

So it will produce these paths:

  • /categories/wishlist/1
  • /categories/wishlist/2
  • /categories/favorites/1

I've tried this but paths returned an empty array:

// pages/categories/[category]/[page].js

export async function getStaticPaths() {
    const paths = []

    // I'm using supabase
    const { data: categories, error } = await supabase.from('categories').select('id, name')

    categories.forEach(async (c) => {
        const { data, error, count } = await supabase
            .from('posts')
            .select('id', { count: 'exact', head: true })
            .eq('category_id', c.id)

        for (let i = 0; i < count; i++) {
            // This should push the path params to the paths variable, but it didn't
            paths.push({
                params: {
                    // For simplicity, 1 post per page
                    page: i + 1,
                    categoryId: c.id,
                    category: c.name
                }
            })
        }
    })

    // It returns an empty array []
    console.log(paths)

    return { paths, fallback: false }
}
7
  • Are you getting the expected data from the supabase's requests? If categories is empty that would explain why you're getting an empty paths array. Commented Oct 13, 2021 at 20:36
  • Of course, I have my data on supabase Commented Oct 14, 2021 at 5:10
  • If you log count to the console, does it return the expected value on each forEach iteration? Commented Oct 14, 2021 at 9:28
  • @juliomalves yes it is, the count exists Commented Oct 14, 2021 at 13:32
  • 1
    Got it, should've use for/of instead of forEach Commented Oct 14, 2021 at 14:05

1 Answer 1

1

Async calling does not work with Array.forEach, instead I use for (category of categories)

Working code:

// pages/categories/[category]/[page].js

export async function getStaticPaths() {
    const paths = []

    // I'm using supabase
    const { data: categories, error } = await supabase.from('categories').select('id, name')

    // Use for/of instead of forEach
    for (category of categories) {
        const { data, error, count } = await supabase
            .from('posts')
            .select('id', { count: 'exact', head: true })
            .eq('category_id', c.id)

        for (let i = 0; i < count; i++) {
            paths.push({
                params: {
                    page: (i + 1).toString(),
                    categoryId: category.id,
                    category: category.name
                }
            })
        }
    })

    return { paths, fallback: false }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.