所以我有这样的代码:
export const getStaticProps: GetStaticProps<HomeProps> = async () => {
const firstCategory = 0;
const { data }: AxiosResponse<MenuItem[]> = await axios.post(
process.env.NEXT_PUBLIC_DOMAIN + '/api/top-page/find',
{
firstCategory,
}
);
return {
props: {
data,
firstCategory,
},
};
};
interface HomeProps extends Record<string, unknown> {
menu: MenuItem[];
firstCategory: number;
}
但是我有一个错误:
Type '() => Promise<{ props: { data: MenuItem[]; firstCategory: number; }; }>' is not assignable to type 'GetStaticProps<HomeProps, ParsedUrlQuery>'.
Type 'Promise<{ props: { data: MenuItem[]; firstCategory: number; }; }>' is not assignable to type 'GetStaticPropsResult<HomeProps> | Promise<GetStaticPropsResult<HomeProps>>'.
Type 'Promise<{ props: { data: MenuItem[]; firstCategory: number; }; }>' is not assignable to type 'Promise<GetStaticPropsResult<HomeProps>>'.
Type '{ props: { data: MenuItem[]; firstCategory: number; }; }' is not assignable to type 'GetStaticPropsResult<HomeProps>'.
Type '{ props: { data: MenuItem[]; firstCategory: number; }; }' is not assignable to type '{ props: HomeProps; revalidate?: number | boolean | undefined; }'.
Types of property 'props' are incompatible.
Property 'menu' is missing in type '{ data: MenuItem[]; firstCategory: number; }' but required in type 'HomeProps'.ts(2322)
我真的不知道ParsedUrlQuery
是什么。我试图进口它(import { ParsedUrlQuery } from 'querystring';
)。但这没有用。
我该怎么办?
HomeProps
包含menu: MenuItem[]
,但随后您试图返回data: MenuItem[]
。如果类型正确,则更新代码以返回如下内容:
return {
props: {
menu: data,
firstCategory,
},
};
或者如果代码正确但类型错误,则更新类型:
interface HomeProps extends Record<string, unknown> {
data: MenuItem[];
firstCategory: number;
}
用域名本身替换process.env.NEXT_PUBLIC_DOMAIN
const { data }: AxiosResponse<MenuItem[]> = await axios.post('https://your domain/api/top-page/find',
{
firstCategory,
}
);
问题是响应不带有变量process.env.NEXT_PUBLIC_DOMAIN