next.js - Nextjs - getServerSideProps 在服务器上不起作用
问题描述
我正在尝试获取详细帖子的数据并将其设置为元标记。
它在 localhost 上使用脚本dev
和运行良好start
,但是当我将代码上传到 EC2 服务器时,我无法从 API 获取这些数据。我不明白为什么它在服务器上不起作用。
这是文件 [slug].jsx:
export default function PostDetail({ postDetail }) {
return (
<>
<Head>
<title>Post detail</title>
<meta
property="title"
content={postDetail.title}
/>
<meta
property="image"
content={postDetail.banner.url}
/>
<meta
property="description"
content={postDetail.description}
/>
</Head>
<div>...</>
</>
)
}
export const getServerSideProps = async ({ query }) => {
try {
const res = await api.postApi.getBySlug(query.slug);
return { props: { postDetail: res.data } };
} catch (err) {
return { props: {} };
}
};
这是我的 package.json 脚本
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start -p 3001",
"export": "next export"
},
感谢您的阅读和您的帮助
解决方案
推荐阅读
- git - Git:提交修改/未跟踪文件的子集
- docker - 如何更改inet以在docker-Compose中运行expo
- android - 片段中的TextView“在空对象引用上”
- javascript - 在 p5.js 中使用 loadImage 的正确方法?
- go - Go build 找不到使用 dep 确保的供应商包
- cassandra - 聊天 - 带有 cassandra 的应用程序(数据模型)
- wordpress - 如果 Webhooks 响应错误,则停止 Ninja Forms 的进度
- android - 如何根据屏幕分辨率调整图像大小并将图像加载到android应用程序(ImageView)中?
- typo3 - TYPO3 CASE with Templavoilà FCE id
- java - 如何通过方法引用访问对象对象内部的方法