在我的 NextJS API 路由中解析响应数据时遇到问题,一直返回 undefined

我正在尝试返回我在 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。

cuijing6019 回答:在我的 NextJS API 路由中解析响应数据时遇到问题,一直返回 undefined

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/7534.html

大家都在问