Shun Yamada
Shun Yamada

Reputation: 979

Next JS + Contentful: Couldn't fetch params data for getting a Contentful data

I would like to use a params fetched from getStaticPaths to get the data in Contentful. However, I'm not getting the params correctly, so I had gotten the wrong data.

Argument of type 'string' is not assignable to parameter of type '{ slug: string; }'.

const getBlogEntries = async () => {
  const { items }: EntryCollection<IBlogFields> = await client.getEntries({
    content_type: "blog"
  })
  return items
}

const getBlogEntry = async ({ slug }: { slug: string }) => {
  console.log('slug', slug)
  
  const { items }: EntryCollection<IBlogFields> = await client.getEntries({
    content_type: "blog",
    "fields.slug[in]": slug,
    limit: 1
  })
  return items
}

export const getStaticPaths: GetStaticPaths = async () => {
  const items = await getBlogEntries()
  const paths = items.map((item) => {
    return {
      params: { slug: item.fields.slug },
    }
  })
  return {
    paths,
    fallback: false,
  }
}

export const getStaticProps: GetStaticProps = async ({
  params,
}) => {
  console.log('params', params!.slug)
  const items = await getBlogEntry(params!.slug as string)
  // the error occurs in `params!.slug`
  return {
    props: {
      blog: items[0],
    },
  }
}

Upvotes: 0

Views: 347

Answers (1)

Shun Yamada
Shun Yamada

Reputation: 979

I got a data:

 const items = await getBlogEntry({ slug: params!.slug})

Upvotes: 1

Related Questions