我尝试使用strapi 和next.js 来制作博客网站。这是用于前端代码的 next.js
import Head from 'next/head'
import styles from '../styles/Home.module.css'
export default function Home({ posts }) {
return (
<div>
{posts &&
posts.map((posts) => (
<div key={post.id}>
<h2>{posts.title}</h2>
</div>
))}
</div>
);
}
export async function getstaticProps() {
//get post from our API
const res = await fetch("http://localhost:1337/posts");
const posts = await res.json();
return {
props: { posts },
};
}
这是 Strapi API Json 数据
[
{
"id": 1,
"title": "Hello Everyone",
"slug": "hello-everyone",
"content": "Hello All.",
"users_permissions_user": {
"id": 4,
"username": "tanha",
"email": "tanha@test.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"role": 1,
"created_at": "2021-03-12T06:02:44.726Z",
"updated_at": "2021-03-12T06:03:54.507Z"
},
"published_at": "2021-03-11T19:37:02.117Z",
"created_at": "2021-03-11T19:36:46.663Z",
"updated_at": "2021-03-12T11:48:57.275Z"
}
]
这里有什么问题?我试过了,但在这里没有发现错误。但无法将数据提取到前端显示。