Trigger Lambda write loop and timeout

It looks like the when running the my trigger type as “after” i am getting a write loop where my query keeps running over and over then timing out at 20s and starting again.

when i run it as a trigger type of “before” i am getting this graphql error.

Code and error below.

Thoughts?

{
  "data": null,
  "errors": [
    {
      "message": "Remote function time out",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "projectUpdate"
      ],
      "code": "IllegalOperationError",
      "details": {
        "remoteFunction": "Remote function time out"
      }
    }
  ]
}

//  8base invoke-local generateGraphs -p src/triggers/generateGraphs/mocks/request.json

const mathjs = require('mathjs');
const gql = require('graphql-tag');
const { spawn } = require('child_process');

const MUTATION = gql`
  mutation MyMutation($id: ID, $computed: JSON) {
    projectUpdate(data: { id: $id, computed: $computed }) {
      computed
      id
    }
  }
`;

module.exports = async (event, ctx) => {

  const { id, projectHorizon } = event.data;

  console.log(event.data);
  console.log(id);
  console.log(projectHorizon);
  let project = [];

  for (let year = 0; year < projectHorizon; year++) {
    const mathTest = mathjs.evaluate(`${projectHorizon} * (${year} + 5.5)`);

    const result = {
      year: year,
      test: mathTest,
    };

    console.log(result);

    project.push(result);
  }


  await ctx.api.gqlRequest(
    MUTATION,
    { id, computed: JSON.stringify(project) },
    {
      waitForResponse: true,
    }
  );

  return {
    data: event.data,
    error: [],
  };
};

Is this tirgger for a Project table?
If so, you just going to an infinite loop.
Updating Project -> trigger called -> you make mutation for projectUpdate -> trigger called -> and so on

1 Like

You can find an example here on how to add data in “before” trigger.
https://docs.8base.com/docs/8base-console/custom-functions/triggers
Look for “You can modify what goes into the database” phrase on the page

1 Like

Hey @sjohnson yeah it looks like you’re call Update after Updater, which then repeats the Update, and so on.

Then in the before trigger, you’re calling Update before Update, which is similar.

Use a before trigger and then just add the data to the update object. Like this.

module.exports = async (event, ctx) => {
  const { projectHorizon } = event.data;

  let project = Array(projectHorizon).fill().map(
    (_, year) => ({
      year: year,
      test: mathjs.evaluate(`${projectHorizon} * (${year} + 5.5)`)
    })
  );

  return {
    data: {
      ...event.data,
      computed: JSON.stringify(project)
    },
    error: [],
  };
};

Wow this is perfect.

1 Like

Right on! Glad it worked :slight_smile: