简体   繁体   English

Next.js getServerSideProps 始终未定义

[英]Next.js getServerSideProps is always undefined

I've been starting with a new Next application and going with functional components instead of class-based wherever possible.我一直从一个新的 Next 应用程序开始,并尽可能使用功能组件而不是基于类的组件。 Following the documentation , I've set up the following with no luck:按照文档,我设置了以下没有运气:

import React from 'react';
import { GetServerSideProps, InferGetServerSidePropsType } from 'next';

export const getServerSideProps: GetServerSideProps = async () => {
    return {
        props: {
            results: [1,2,3],
        },
    };
};

function Page({results}: InferGetServerSidePropsType<typeof getServerSideProps>) {
    console.log(results) // undefined
    return (
        <div className="app__page header">page here</div>
    )
}

export default Page;

Regardless of what I've tried I can't get any data back from getServerSideProps , any help would be greatly appreciated!无论我尝试了什么,我都无法从getServerSideProps取回任何数据,我们将不胜感激任何帮助!

Nothing wrong with the code but it could be in a wrong place.代码没有错,但它可能在错误的地方。

Make sure that is a Next.js page and located in pages directory as getServerSideProps only allowed in a page.确保这是一个 Next.js 页面并位于pages目录中,因为getServerSideProps仅允许在页面中。

You should see printed [1,2,3] on server-side and in the browser console.您应该在服务器端和浏览器控制台中看到打印的[1,2,3]

Possible you used custom _app page (eg pankod boilerplate), and it not provide correct ServerSideProps to component.可能您使用了自定义 _app 页面(例如 pankod 样板),并且它没有为组件提供正确的 ServerSideProps。

Also remember that current version of Next.js with custom _app currently does not support Next.js Data Fetching methods like getStaticProps or getServerSideProps.另请记住,当前版本的 Next.js 与自定义 _app 目前不支持 Next.js 数据获取方法,如 getStaticProps 或 getServerSideProps。

Read more at https://nextjs.org/docs/advanced-features/custom-apphttps 了解更多信息://nextjs.org/docs/advanced-features/custom-app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM