【问题标题】:Vercel Error 500 and Unexpected token I in json position at 0Vercel 错误 500 和意外令牌 I 在 json 位置为 0
【发布时间】:2021-07-20 14:50:02
【问题描述】:

我正在做下一个项目,我有一个内容丰富的服务器。 我可以在本地完美地发布内容,但在 vercel 上我收到错误: json 位置中的意外令牌 I 位于 0

这是我发布条目的代码 index.js 中的代码


    const handleSubmitPotato = async (data) => {
    data.mouth = assets.mouth;
    data.pant = assets.pant;
    data.eye = assets.eye;
    data.nose = assets.nose;
    data.hat = assets.hat;

    const response = await fetch("/api/sendpotatoe", {
      method: "POST",
      body: JSON.stringify(data),
      headers: {
        "Content-Type": "application/json",
      },
    });
    // console.log(response);
    await response.json();
    console.log(response);
  };

api/sendpotato.js 中的代码

const contentful = require("contentful-management");

export default async (req, res) => {
  // console.log(req.body);
  // console.log(req.body.mouth);

  const mouthid = req.body.mouth.sys.id;
  const hatid = req.body.hat.sys.id;
  const eyeid = req.body.eye.sys.id;
  const noseid = req.body.nose.sys.id;
  const pantid = req.body.pant.sys.id;

  if (req.method === "POST") {
    try {
      const client = contentful.createClient({
        accessToken: `${process.env.CONTENTFUL_MANAGEMENT_TOKEN}`,
      });

      let space = await client.getSpace(process.env.CONTENTFUL_SPACE_ID);
      let environment = await space.getEnvironment("master");

      const response = await environment.createEntry("potatoes", {
        fields: {
          name: {
            "en-US": req.body.name,
          },
          potatoname: {
            "en-US": req.body.potatoname,
          },
          message: {
            "en-US": req.body.message,
          },
          mouth: {
            "en-US": {
              sys: {
                id: mouthid,
                type: "Link",
                linkType: "Entry",
              },
            },
          },
          hat: {
            "en-US": {
              sys: {
                id: hatid,
                type: "Link",
                linkType: "Entry",
              },
            },
          },
          eye: {
            "en-US": {
              sys: {
                id: eyeid,
                type: "Link",
                linkType: "Entry",
              },
            },
          },
          nose: {
            "en-US": {
              sys: {
                id: noseid,
                type: "Link",
                linkType: "Entry",
              },
            },
          },
          pant: {
            "en-US": {
              sys: {
                id: pantid,
                type: "Link",
                linkType: "Entry",
              },
            },
          },
        },
      });

      await response.publish();
      await res.status(200).send({ status: "OK" });
      return response;
    } catch (err) {
      console.log(`${err}`);
      res.status(500).end(`Something went wrong: ${e}`);
    }
  }
};

为什么我在 Vercel 上而不是在本地得到这个? 我已将所有环境变量都放在 vercel 上。 我已经放了 res.status(500).end 并且我认为这会有所帮助。

在我的 vercel 回复标签下,它只是说: 内部服务器错误。

我知道这个问题被问了很多,但我发现它很难调试。 感谢您的帮助,所有帮助我理解的提示都是一种帮助!

【问题讨论】:

  • 错误的 API 响应通常是 headers 的问题。仔细检查那些。

标签: node.js post next.js contentful unexpected-token


【解决方案1】:

我发现了如何调试它。我查看了 vercel 上的功能日志,发现我在环境变量中复制粘贴了错误的标记。感谢那些看过我的帖子或想出答案的人

(我已经检查了我的标题,谢谢@serraosays)

【讨论】:

    猜你喜欢
    • 2022-07-11
    • 1970-01-01
    • 2016-12-13
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2020-04-23
    相关资源
    最近更新 更多