Why would a query param be undefined in NextJS?

I’m calling a page withRouter(Page) and expect the variable for the page (the page is called [category].js) to be present on initial page load. Query itself is there, the key is there, but the value is ‘undefined.’ There seem to be a few calls to getInitialProps on the server side with 2/3 being undefined.

The react component has a constructor, etc. it’s not a functional component.

This is my current getInitialProps:

Category.getInitialProps = async ({ req, query }) => {
  let authUser = req && req.session && req.session.authUser
  let categoryData = {}
  let categoryItemData = {}
  let category = query.category
  if(category){
    let res = await fetch(url1,
    {
        method: 'POST',
        credentials: 'include',
    })
    categoryData = await res.json();
    let categoryItemsRes =  await fetch(url2, 
        {
            method: 'POST',
            credentials: 'include',
        })
    categoryItemData = await categoryItemsRes.json();
  }

  return { query, authUser, categoryData, categoryItemData }
}

77 thoughts on “Why would a query param be undefined in NextJS?”

Leave a Comment