Unexpected token u in JSON at position 0 in trigger

I created a trigger that has to update a record after creating a record, when I invoke in my terminal , it works perfectly, but when I create a new record it shows me "“Unexpected token u in JSON at position 0"”


What could be the problem?

Hello!

We’ve heard you. We’ll give you a response as soon as possible.

Lada Kokotova | Technical Support Engineer

ok, thanks
I will check it as soon as you tell me

Could you send me your trigger code, please? This code will help me find a personalized solution to your issue.

Lada Kokotova | Technical Support Engineer

Sure here is the link : https://drive.google.com/drive/folders/16d3b1GEVp8pSif7kSmI-CIiEmoZE2R_x?usp=sharing

Thanks! I’ll check what’s wrong and let you know.

Lada Kokotova | Technical Support Engineer

Just to be sure that we are on the same page. Does the code you sent work wrong?

If so, seems like error fails from
const eventData = JSON.parse(event.body)(63 line).

Pay attention to the event format. You try to get the property with the name “body” from the event.
You can read more information here:
https://docs.8base.com/docs/8base-console/custom-functions/triggers/

To debug the code you can wrap the line into try catch block and send the error to console log.

Here’s an example:

export default async (event, ctx) => {
  let eventData;
  try {
    eventData = JSON.parse(event.body);
  } catch (e) {
    console.log(e);
  }
  let response;

  try {
    response = await ctx.api.gqlRequest(transactionUpdate, { ID: eventData.ID, gatewayFee: gatewayFee });

    /* Handle errors for failed GraphQL mutation */
  } catch (e) {
    return responseBuilder(422, "Failed to update transaction");
  }
  return {
    data: {
      response,
    },
  };
};

Lada Kokotova | Technical Support Engineer