在我的 _document.js 中,我配置了一些样式。这是我的_document.js
import React from "react";
import Document, {Html ,Head, Main, NextScript } from "next/document";
import { ServerStyleSheets } from "@material-ui/styles";
class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>
<meta charSet="utf-8" />
<meta name="theme-color" content="#000000" />
<link rel="shortcut icon" href={require("assets/img/favicon.png")} />
<link
rel="apple-touch-icon"
sizes="76x76"
href={require("assets/img/apple-icon.png")}
/>
{/* Fonts and icons */}
<link
rel="stylesheet"
type="text/css"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons"
/>
<link
href="https://use.fontawesome.com/releases/v5.0.10/css/all.css"
rel="stylesheet"
/>
</Head>
<body>
<div id="page-transition"></div>
<Main />
<NextScript />
</body>
</Html>
);
}
}
MyDocument.getInitialProps = async ctx => {
// Resolution order
//
// On the server:
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. document.getInitialProps
// 4. app.render
// 5. page.render
// 6. document.render
//
// On the server with error:
// 1. document.getInitialProps
// 2. app.render
// 3. page.render
// 4. document.render
//
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheets.collect(<App {...props} />)
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [
<React.Fragment key="styles">
{initialProps.styles}
{sheets.getStyleElement()}
</React.Fragment>
]
};
};
export default MyDocument;
现在,当我尝试使用 getStaticProps 从 API 获取数据时,它给了我一个服务器错误,
TypeError: 'Cannot read property of 'data' undefined'
我在 index.js 中的代码
export const getStaticProps = async () => {
const res = await fetch("http://localhost:8055/items/gallery2");
const dataa = await res.json();
return {
props: { gallery: dataa },
};
};
当我尝试做的时候,
{gallery.data.map(image_url => (
<div>
</div>
))
它给了我下面提到的类型错误。 我应该怎么做才能让它开始工作? PS:我在 index.js 中使用 getStaticProps。我不知道为什么会出现这个错误,请帮我解决这个问题。
谢谢! 编辑1: 我的控制台错误:
Uncaught at Cardd (file://D:\projects\nextjs-material-kit-master\.next\server\pages\components.js:15365:14)
at processChild (file://D:\projects\nextjs-material-kit-master\node_modules\react-dom\cjs\react-dom-server.node.development.js:3043:14)
at resolve (file://D:\projects\nextjs-material-kit-master\node_modules\react-dom\cjs\react-dom-server.node.development.js:2960:5)
at ReactDOMServerRenderer.render (file://D:\projects\nextjs-material-kit-master\node_modules\react-dom\cjs\react-dom-server.node.development.js:3435:22)
at ReactDOMServerRenderer.read (file://D:\projects\nextjs-material-kit-master\node_modules\react-dom\cjs\react-dom-server.node.development.js:3373:29)
at renderToString (file://D:\projects\nextjs-material-kit-master\node_modules\react-dom\cjs\react-dom-server.node.development.js:3988:27)
at renderPage (file://D:\projects\nextjs-material-kit-master\node_modules\next\dist\next-server\server\render.js:54:851)
at Object.ctx.renderPage (file://D:\projects\nextjs-material-kit-master\.next\server\pages\_document.js:1629:26)
at Function.getInitialProps (file://D:\projects\nextjs-material-kit-master\.next\server\pages\_document.js:699:19)
at Function.module.exports../pages/_document.js.MyDocument.getInitialProps (file://D:\projects\nextjs-material-kit-master\.next\server\pages\_document.js:1640:83)
at loadGetInitialProps (file://D:\projects\nextjs-material-kit-master\node_modules\next\dist\next-server\lib\utils.js:5:101)
at renderToHTML (file://D:\projects\nextjs-material-kit-master\node_modules\next\dist\next-server\server\render.js:54:1142)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async (file://D:\projects\nextjs-material-kit-master\node_modules\next\dist\next-server\server\next-server.js:109:97)
at async __wrapper (file://D:\projects\nextjs-material-kit-master\node_modules\next\dist\lib\coalesced-function.js:1:330)
答案 0 :(得分:0)
添加 try/catch
块并检查 api 请求是否失败:
export const getStaticProps = async () => {
let dataa = {data: []};
try {
const res = await fetch("http://localhost:8055/items/gallery2");
dataa = await res.json();
} catch(err) {
console.log("request failed: ", err);
}
return {
props: { gallery: dataa },
};
};
答案 1 :(得分:0)
添加“道具”。或“this.props”。在画廊之前再次测试
{
props.gallery.data.map(image_url) => {return (
<div></div>
)}
}