ブログのカテゴリ別一覧ページを作成したいのだが下記エラーが発生する。
しかしエラーが出た状態でリロードすると該当の記事一覧は取得できている。
カテゴリ一覧以外では問題なくCMSから記事は取得できております。
MicroCMSのプランはhobbyです。
エラー内容
Unhandled Runtime Error Error: parameter is required (check serviceDomain and apiKey) Source libs/client.ts (3:35) @ eval 1 | import { createClient } from 'microcms-js-sdk' 2 | > 3 | export const client = createClient({ | ^ 4 | serviceDomain: process.env.SERVICE_DOMAIN ?? '', 5 | apiKey: process.env.API_KEY ?? '', 6 | })
/pages/blog/[category]/page/[id.js](エラー該当ページ)
import { client } from '../../../../libs/client'; import { PER_PAGE } from '../../../../components/Pagination'; import { PageList } from '../../../../components/PageList'; import { range } from '../../../../modules/utils'; import type { GetStaticProps, GetStaticPaths } from 'next' import { blogsTypes, categoryType } from '../../../../types/types'; interface props extends blogsTypes { totalCount: number currentPage: number } // pages/blog/[id].js export default function BlogPageId({ contents, totalCount, currentPage }: props) { return ( <main> <PageList contents={contents} totalCount={totalCount} currentPage={currentPage} /> </main> ); } export const getAllCategoryPagePaths = async () => { const resCategory = await client.get({endpoint: 'categories'}) const paths: string[] = await Promise.all( resCategory.contents.map((category: categoryType) => { const result = client .get({ endpoint: 'blogs', queries: { filters: `category[equals]${category.id}` } }) .then(({ totalCount }) => { return range(1, Math.ceil(totalCount / PER_PAGE)).map((repo) => `/blog/${category.id}/page/${repo}` ) }) .catch((err) => console.log(err)); return result }) ) return paths.flat() } export const getStaticPaths: GetStaticPaths = async () => { const paths = await getAllCategoryPagePaths() return { paths, fallback: false } } // データを取得 export const getStaticProps: GetStaticProps = async (context) => { const { params } = context const id = context.params!.id; const data = await client.get({ endpoint: 'blogs', queries: { limit: PER_PAGE, offset: (Number(id) - 1) * PER_PAGE, filters: `category[equals]${params!.category}` } }) return { props: { contents: data.contents, totalCount: data.totalCount, currentPage: id, currentCategory: params!.category } } }
バージョン
"dependencies": { "microcms-js-sdk": "^2.0.0", "next": "12.1.6", "react": "18.2.0", "react-dom": "18.2.0" }, "devDependencies": { "@types/node": "17.0.43", "@types/react": "18.0.12", "@types/react-dom": "18.0.5", "eslint": "8.17.0", "eslint-config-next": "12.1.6", "typescript": "4.7.3" }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。