やりたいこと
Next.jsでブログを作成したいです.
GitHub APIを使用して外部から記事の情報を取得してソートしたいのですが、上手く行かないので、どなたかご教授頂けると幸いです.
ソースコード
lib/posts.js
javascript
1export async function getSortedPostsData() { 2 // Get file names under /posts 3 // const fileNames = fs.readdirSync(postsDirectory) 4 const repoUrl = "https://api.github.com/repos/satorun082/article/contents" 5 const response = await fetch(repoUrl) 6 const files = await response.json() 7 const fileNames = files.map(file => file.name) 8 9 const allPostsData = fileNames.map(async (fileName) => { 10 // Remove ".md" from file name to get id 11 const id = fileName.replace(/.md$/, '') 12 13 // Read markdown file as string 14 const repoUrl_tmp = `https://api.github.com/repos/satorun082/article/contents/${id}.md` 15 const response_tmp = await fetch(repoUrl_tmp) 16 const file_tmp = await response_tmp.json() 17 const fileContents = base64.decode(file_tmp.content) 18 19 // Use gray-matter to parse the post metadata section 20 const matterResult = matter(fileContents) 21 22 // Combine the data with the id 23 return { 24 id, 25 ...matterResult.data 26 } 27 }) 28 // Sort posts by date 29 return await allPostsData.sort(({ date: a }, { date: b }) => { 30 if (a < b) { 31 return 1 32 } else if (a > b) { 33 return -1 34 } else { 35 return 0 36 } 37 }) 38}
index.tsx
import { GetStaticProps } from 'next' import Head from 'next/head' import Layout, { siteTitle } from '../components/layout' import utilStyles from '../styles/utils.module.css' import { getSortedPostsData } from '../lib/posts' import Link from 'next/link' import Date from '../components/date' export const getStaticProps: GetStaticProps = async() => { const allPostsData = await getSortedPostsData() return { props: { allPostsData } } } type Props = { allPostsData: { id: string title: string date: string }[] } export default function Home({ allPostsData }: Props) { return ( <Layout home> <Head> <title>{siteTitle}</title> </Head> <section className={utilStyles.headingMd}> <p>Im nakadats and Im learning English to make my brondy girl friends</p> <p> (This is a sample website - you’ll be building a site like this on{' '} <a href="https://nextjs.org/learn">our Next.js tutorial</a>.) </p> </section> <section className={`${utilStyles.headingMd} ${utilStyles.padding1px}`}> <h2 className={utilStyles.headingLg}>Blog</h2> <ul className={utilStyles.list}> {allPostsData.map(({ id, date, title }) => ( <li className={utilStyles.listItem} key={id}> <Link href={`/${id}`}> <a>{title}</a> </Link> <br /> <small className={utilStyles.lightText}> <Date dateString={date} /> </small> </li> ))} </ul> </section> </Layout> ) }
エラー内容
error
1Server Error 2Error: Error serializing `.allPostsData[0]` returned from `getStaticProps` in "/". 3Reason: `object` ("[object Promise]") cannot be serialized as JSON. Please only return JSON serializable data types. 4 5This error happened while generating the page. Any console logs will be displayed in the terminal window. 6Call Stack 7isSerializable 8file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (64:15) 9<unknown> 10file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (56:24) 11Array.every 12<anonymous> 13isSerializable 14file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (54:23) 15<unknown> 16file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (46:66) 17Array.every 18<anonymous> 19isSerializable 20file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (43:39) 21Object.isSerializableProps 22file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (66:12) 23Object.renderToHTML 24file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/server/render.js (430:97) 25runMicrotasks 26<anonymous>
参考サイト
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/14 06:43
2021/08/14 11:26