下一个JS: getServerSideProps不能获取cookie



我想返回两个cookie,但只有一个是只返回令牌cookie,而密钥cookie不能使用

export async function getServerSideProps(ctx) {

const cookies = ctx.req.headers.cookie;
const cookie = cookies.split(";");
const [typeToken, token] = cookie[0].split("=");
const [typeKey, key] = cookie[1].split("=");
console.log(token); //Work
console.log(key); //Work
return { props: { token, key } };
}

在getServerSide函数中,它可以取key cookie,但是

export default function Index({ token, key }) {
console.log(token); //work
console.log(key); //not work

在这个函数中只能得到令牌cookie,不能得到密钥cookie

你不能在React中有一个名为key的道具,它会被覆盖。将它命名为其他东西,你应该可以访问它。详见reactjs.org/warnings/special-props.html

最新更新