【发布时间】:2021-12-25 22:30:29
【问题描述】:
我正在尝试使用 Strapi+ graphql 构建一个 NextJs 博客网站。但是按照官方strapi视频源给出的步骤后,我在home.jsx文件中遇到了这个问题......
Error: Error serializing props returned from `getStaticProps` in "/".
Reason: Props must be returned as a plain object from getStaticProps: `{ props: { ... } }` (received: `[object Undefined]`).
这是 Home.jsx 页面代码:
import { Fragment } from "react";
import HomePage from "./home/home";
import { ApolloClient, InMemoryCache, gql } from "@apollo/client";
export async function getStaticProps() {
const client = new ApolloClient({
url: "http:/localhost:1337/graphql",
cache: new InMemoryCache(),
});
const GET_BLOGS = gql`
query getBlogs {
blogs {
id: string
title: string
}
}
`;
let getData = client.query({ query: GET_BLOGS });
console.log("" + getData);
return { props: getData.blogs };
}
export default function Home({ blogs }) {
if (blogs) return console.log(blogs);
return (
<Fragment>
<HomePage />
</Fragment>
);
}
可能是 next 和 Strapi package.json 文件的依赖问题,所以这里是两者的依赖关系:
Nextjs package.json 依赖项-
"dependencies": {
"@apollo/client": "^3.4.17",
"axios": "^0.24.0",
"graphql": "^16.0.1",
"next": "12.0.2",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-toastify": "^8.1.0"
},
Strapi 项目依赖:
"dependencies": {
"knex": "0.21.18",
"sqlite3": "5.0.0",
"strapi": "3.6.8",
"strapi-admin": "3.6.8",
"strapi-connector-bookshelf": "3.6.8",
"strapi-plugin-content-manager": "3.6.8",
"strapi-plugin-content-type-builder": "3.6.8",
"strapi-plugin-email": "3.6.8",
"strapi-plugin-graphql": "3.6.8",
"strapi-plugin-i18n": "3.6.8",
"strapi-plugin-upload": "3.6.8",
"strapi-plugin-users-permissions": "3.6.8",
"strapi-utils": "3.6.8"
},
"engines": {
"node": ">=10.16.0 <=14.x.x",
"npm": "^6.0.0"
},
【问题讨论】:
-
修正错字,
http:/localhost:1337/graphql到http://... -
console.log(getData)在终端输出什么?
标签: graphql next.js apollo-client strapi graphql-js