teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

改善した後のエラー内容

2021/08/14 06:39

投稿

HelloQ
HelloQ

スコア81

title CHANGED
File without changes
body CHANGED
@@ -7,6 +7,8 @@
7
7
 
8
8
  [GitHub](https://github.com/satorun082/nextjs-blog)
9
9
 
10
+ `lib/posts.js`
11
+
10
12
  ```javascript
11
13
  export async function getSortedPostsData() {
12
14
  // Get file names under /posts
@@ -16,14 +18,14 @@
16
18
  const files = await response.json()
17
19
  const fileNames = files.map(file => file.name)
18
20
 
19
- const allPostsData = fileNames.map(fileName => {
21
+ const allPostsData = fileNames.map(async (fileName) => {
20
22
  // Remove ".md" from file name to get id
21
23
  const id = fileName.replace(/.md$/, '')
22
24
 
23
25
  // Read markdown file as string
24
26
  const repoUrl_tmp = `https://api.github.com/repos/satorun082/article/contents/${id}.md`
25
- const response_tmp = fetch(repoUrl_tmp)
27
+ const response_tmp = await fetch(repoUrl_tmp)
26
- const file_tmp = response_tmp.json()
28
+ const file_tmp = await response_tmp.json()
27
29
  const fileContents = base64.decode(file_tmp.content)
28
30
 
29
31
  // Use gray-matter to parse the post metadata section
@@ -35,7 +37,6 @@
35
37
  ...matterResult.data
36
38
  }
37
39
  })
38
-
39
40
  // Sort posts by date
40
41
  return await allPostsData.sort(({ date: a }, { date: b }) => {
41
42
  if (a < b) {
@@ -49,29 +50,98 @@
49
50
  }
50
51
  ```
51
52
 
53
+ `index.tsx`
54
+
55
+ ```
56
+ import { GetStaticProps } from 'next'
57
+ import Head from 'next/head'
58
+ import Layout, { siteTitle } from '../components/layout'
59
+ import utilStyles from '../styles/utils.module.css'
60
+ import { getSortedPostsData } from '../lib/posts'
61
+ import Link from 'next/link'
62
+ import Date from '../components/date'
63
+
64
+ export const getStaticProps: GetStaticProps = async() => {
65
+ const allPostsData = await getSortedPostsData()
66
+ return {
67
+ props: {
68
+ allPostsData
69
+ }
70
+ }
71
+ }
72
+
73
+ type Props = {
74
+ allPostsData: {
75
+ id: string
76
+ title: string
77
+ date: string
78
+ }[]
79
+ }
80
+
81
+ export default function Home({ allPostsData }: Props) {
82
+ return (
83
+ <Layout home>
84
+ <Head>
85
+ <title>{siteTitle}</title>
86
+ </Head>
87
+ <section className={utilStyles.headingMd}>
88
+ <p>Im nakadats and Im learning English to make my brondy girl friends</p>
89
+ <p>
90
+ (This is a sample website - you’ll be building a site like this on{' '}
91
+ <a href="https://nextjs.org/learn">our Next.js tutorial</a>.)
92
+ </p>
93
+ </section>
94
+
95
+ <section className={`${utilStyles.headingMd} ${utilStyles.padding1px}`}>
96
+ <h2 className={utilStyles.headingLg}>Blog</h2>
97
+ <ul className={utilStyles.list}>
98
+ {allPostsData.map(({ id, date, title }) => (
99
+ <li className={utilStyles.listItem} key={id}>
100
+ <Link href={`/${id}`}>
101
+ <a>{title}</a>
102
+ </Link>
103
+ <br />
104
+ <small className={utilStyles.lightText}>
105
+ <Date dateString={date} />
106
+ </small>
107
+ </li>
108
+ ))}
109
+ </ul>
110
+ </section>
111
+ </Layout>
112
+ )
113
+ }
114
+ ```
115
+
52
116
  # エラー内容
53
117
 
54
- ```
118
+ ```error
55
119
  Server Error
56
- TypeError: response_tmp.json is not a function
120
+ Error: Error serializing `.allPostsData[0]` returned from `getStaticProps` in "/".
121
+ Reason: `object` ("[object Promise]") cannot be serialized as JSON. Please only return JSON serializable data types.
57
122
 
58
123
  This error happened while generating the page. Any console logs will be displayed in the terminal window.
59
- Source
60
- .next/server/pages/index.js (26:34) @ <unknown>
61
-
62
- 24 | const repoUrl_tmp = `https://api.github.com/repos/satorun082/article/contents/${id}.md`
63
- 25 | const response_tmp = fetch(repoUrl_tmp)
64
- > 26 | const file_tmp = response_tmp.json()
65
- | ^
66
- 27 | const fileContents = base64.decode(file_tmp.content)
67
- 28 |
68
- 29 | // Use gray-matter to parse the post metadata section
69
124
  Call Stack
125
+ isSerializable
126
+ file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (64:15)
127
+ <unknown>
128
+ file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (56:24)
129
+ Array.every
70
- getSortedPostsData
130
+ <anonymous>
131
+ isSerializable
71
- .next/server/pages/index.js (19:33)
132
+ file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (54:23)
133
+ <unknown>
134
+ file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (46:66)
135
+ Array.every
136
+ <anonymous>
137
+ isSerializable
138
+ file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (43:39)
72
- async getStaticProps
139
+ Object.isSerializableProps
73
- .next/server/pages/index.js (10:23)
140
+ file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/lib/is-serializable-props.js (66:12)
74
- Show collapsed frames
141
+ Object.renderToHTML
142
+ file:///mnt/c/Users/minolab/Desktop/nextjs-blog/node_modules/next/dist/server/render.js (430:97)
143
+ runMicrotasks
144
+ <anonymous>
75
145
  ```
76
146
 
77
147
  # 参考サイト

1

題目の変更

2021/08/14 06:39

投稿

HelloQ
HelloQ

スコア81

title CHANGED
@@ -1,1 +1,1 @@
1
- Next.jsにおけるServer Error
1
+ Next.jsにおける外部APIの使用
body CHANGED
File without changes