Hannah
Hannah

Reputation: 35

Vercel Error 500 and Unexpected token I in json position at 0

I am working on a next project and I have a contentful server. I can post an entry to contentful perfectly locally but on vercel I get the error: Unexpected token I in json position at 0

Here is my code to post an entry code in 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);
  };

code in 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}`);
    }
  }
};

Why do I get this on Vercel and not locally? I have put all my environment variabeles on vercel. I have put res.status(500).end and I thought that would help.

Under my response tab on vercel it just says: Internal server error.

I know this question is asked a lot, but I find it very difficult to debug. Thank you for helping, all tips to help me understand this is a kind help!

Upvotes: 1

Views: 1054

Answers (1)

Hannah
Hannah

Reputation: 35

I have found out how I can debug this. I have looked under functional logs on vercel and found out that I had copy pasted the wrong token in my environment variabeles. Thank you for the ones that looked to my post or came up with an answer

(I had already checked my headers, thanks @serraosays )

Upvotes: 0

Related Questions