试图在getServerSideProps
中插入一个条件:
export async function getServerSideProps(context) {
const jwt = parseCookies(context).jwt || null;
if (jwt) {
const user = parseJwt(jwt);
const userId = user.id;
console.log(userId);
} else {
return {};
}
return {
props: { jwt, userId },
};
}
得到错误:
错误:序列化从"/account"
getServerSideProps
返回的道具时出错。原因:Props必须作为一个普通对象从getServerSideProps:{ props: { ... } }
返回。
你会怎么做?
getServerSideProps
应该以{ props: {} }
的形式返回一个真正特定的对象,您所展示的代码没有考虑到这一点。试试改成:
export async function getServerSideProps(context) {
const jwt = parseCookies(context).jwt || null;
if (jwt) {
const user = parseJwt(jwt);
const userId = user.id;
return {
props: { jwt, userId },
};
} else {
return {
props: {},
};
}
}