Nextjs - getServerSideProps 在服务器上不起作用

时间:2021-07-25 21:10:07

标签: next.js

我正在尝试获取详细帖子的数据并将其设置为元标记。

使用脚本 devstart 在 localhost 上运行良好,但是当我将代码上传到 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"
},

感谢您的阅读和帮助

0 个答案:

没有答案
相关问题