【发布时间】:2021-08-26 11:51:39
【问题描述】:
我的“getStaticPaths”函数有问题。在 getStaticPaths 中未将必需参数 (id) 作为字符串提供。连我都用过id:users.id.toString()
export default function usersID({ users }) {
return (
<div>
<h1> {mobile.name} </h1>
</div>
);
}
export const getStaticPaths = async () => {
const res = await fetch(`http://jsonplaceholder.typicode.com/users`);
const data = await res.json();
const paths = data.map((users) => {
return {
params: {
id: users.id.toString()
},
};
});
return {
paths,
fallback: false,
};
};
export const getStaticProps = async (context) => {
const id = context.params.id;
const res = await fetch(`http://jsonplaceholder.typicode.com/users/${params.id}` );
const data = await res.json();
return {
props: { users: data },
};
};
【问题讨论】:
-
您确定定义了
users吗?你退出users.id.toString()了吗? -
是的!我也尝试过使用其他 API,但仍然显示错误!