我正在尝试返回我在 NextJS 中设置的 REST API 的虚拟数据,但它一直返回未定义。我被困在这里很长时间了,我不知道如何解决它。
export default function userHandler(req,res) {
const {
query: { id,name },method,} = req;
switch (method) {
case "GET":
res.status(200).json({ id,name: `User ${id} ${name}` });
console.log(`${req.name}`);
break;
case "PUT":
res.status(200).json({ id,name: name || `User ${id}` });
break;
default:
res.setHeader("Allow",["GET","PUT"]);
res.status(405).end(`Method ${method} Not Allowed`);
}
}
我的 localhost:3000/user/1 返回“User 1 undefined”,我的 console.log 也返回 undefined。
如何让“未定义”返回实际值? 我试过 Json.stringify 和 JSON.parse。