0

I'm running into problems while trying to execute my first Appwrite Cloud function from the client. The function should create a new database with the same ID and team name as the team that was just created:

Error:

An internal curl error has occurred within the executor! Error Msg: Operation timed out

From the log:

No logs recorded

The cloud function:

const sdk = require("node-appwrite");
module.exports = async function (req, res) {
  const client = new sdk.Client();
  const database = new sdk.Databases(client);
  if (
    !req.variables.APPWRITE_FUNCTION_ENDPOINT ||
    !req.variables.APPWRITE_FUNCTION_API_KEY
  ) {
    console.warn(
      "Environment variables are not set. Function cannot use Appwrite SDK."
    );
  } else {
    client
      .setEndpoint(req.variables.APPWRITE_FUNCTION_ENDPOINT)
      .setProject(req.variables.APPWRITE_FUNCTION_PROJECT_ID)
      .setKey(req.variables.APPWRITE_FUNCTION_API_KEY)
      .setSelfSigned(true);
  }
  const payload = JSON.parse(req.variables.APPWRITE_FUNCTION_DATA);
  const promise = database.create(payload.teamId, payload.teamName);
  promise.then(
    function (response) {
      req.json(response);
    },
    function (error) {
      req.json(error);
    }
  );
};

Calling the function from the client:

import { useFunction } from "react-appwrite";

const setupTeamDatabase = useFunction("647687110d37ce2558c1");

const teamDetails = JSON.stringify({
  teamId: createTeam.$id,
  teamName: createTeam.name,
});
const createDatabase = await setupTeamDatabase.mutateAsync(teamDetails);

From appwrite.json:

{
  "$id": "647687110d37ce2558c1",
  "name": "setupTeam",
  "runtime": "node-16.0",
  "path": "functions/setupTeam",
  "entrypoint": "src/index.js",
  "ignore": ["node_modules", ".npm"],
  "execute": ["users"],
  "events": [],
  "schedule": "",
  "timeout": 15
}

package.json

{
    "node-appwrite": "^9.0.0"
    "appwrite": "^11.0.0",
    "react-appwrite": "^0.4.2"
}
Brad Stewart
  • 333
  • 2
  • 15

1 Answers1

0

An internal curl error has occurred within the executor! Error Msg: Operation timed out

This usually occurs because there is some error in the function or your code is timing out. Some things you should consider doing:

  1. Ensure the APPWRITE_FUNCTION_ENDPOINT and APPWRITE_FUNCTION_API_KEY function variables are created and correct.
  2. Use res.json() rather than req.json().
  3. Don't pass error to res.json() because it might not serialize correctly.
  4. Use async/try/await/catch rather than promise/then/catch to make the code cleaner and clearer
  5. If your function runs for 15 seconds before erroring, increase the timeout to 4 minutes so the execution doesn't time out, and you can catch the error.
Steven Nguyen
  • 452
  • 4
  • 4