Back

[SOLVED] Function is not executed

  • 0
  • Functions
  • Flutter
hortigado
25 May, 2023, 02:16

Hello, I have a problem. I have created a function in dart but when it is executed, it is not called. I have tried to run it from the appwrite console but it doesn't work either, it just throws me this error.

TL;DR
The user is experiencing an issue where their function is not being executed. They have tried running it from the Appwrite console but it still doesn't work. Other users suggest adding a try/catch block and using res.json() properly. The user updates their code accordingly, but the function still doesn't execute. They then mention that they have created a new function with the same code and it works. The problem seems to be resolved, but the cause is not identified.
Drake
25 May, 2023, 02:17

This usually happens because there's an exception thrown in your code.

Did you start with the start template created by the Appwrite CLI?

hortigado
25 May, 2023, 02:18

`Future<void> start(final req, final res) async { final client = Client(); print(0000); // Uncomment the services you need, delete the ones you don't // final account = Account(client); // final avatars = Avatars(client); // final database = Databases(client); // final functions = Functions(client); // final health = Health(client); // final locale = Locale(client); // final storage = Storage(client); final teams = Teams(client); // final users = Users(client); final payload = jsonDecode(req.payload == '' ? '{}' : req.payload);

if (req.variables['APPWRITE_FUNCTION_ENDPOINT'] == null || req.variables['APPWRITE_FUNCTION_API_KEY'] == null) { print( "Environment variables are not set. Function cannot use Appwrite SDK."); res.json({ 'success': true, 'message': "Environment variables are not set. Function cannot use Appwrite SDK.", }, status: 200); } else { client .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT']) .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID']) .setKey(req.variables['APPWRITE_FUNCTION_API_KEY']) .setSelfSigned(status: true); } final String teamId = payload['teamId']; final String userTo = payload['userTo']; /* await teams.create(teamId: teamId, name: "chat:$teamId"); */

await teams.createMembership( teamId: teamId, email: userTo, roles: [], url: req.variables['APPWRITE_FUNCTION_ENDPOINT']); res.json(success: true); } `

hortigado
25 May, 2023, 02:18

yes I edited it a bit, that's how I have it now

hortigado
25 May, 2023, 02:19
Drake
25 May, 2023, 02:20

Make sure to use 3 back ticks instead of 1 for multi line code

Drake
25 May, 2023, 02:21

I suggest wrapping everything in a big try/catch to make sure there are no uncaught exceptions

Drake
25 May, 2023, 02:21

That res.json also looks incorrect

hortigado
25 May, 2023, 02:23

Yes, I just implemented the res.json to test if it was the error due to lack of that, but it didn't work for me either or I got another error

Drake
25 May, 2023, 02:26

I don't know what you mean

hortigado
25 May, 2023, 02:27
TypeScript
  try {
    final client = Client();
    print(0000);
    final teams = Teams(client);
    final payload = jsonDecode(req.payload == '' ? '{}' : req.payload);

    if (req.variables['APPWRITE_FUNCTION_ENDPOINT'] == null ||
        req.variables['APPWRITE_FUNCTION_API_KEY'] == null) {
      print(
          "Environment variables are not set. Function cannot use Appwrite SDK.");
      res.json({
        'success': false,
        'message':
            "Environment variables are not set. Function cannot use Appwrite SDK.",
      }, status: 500);
    } else {
      client
          .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
          .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
          .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
          .setSelfSigned(status: true);
    }
    final String teamId = payload['teamId'];
    final String userTo = payload['userTo'];
    await teams.createMembership(
        teamId: teamId,
        email: userTo,
        roles: [],
        url: req.variables['APPWRITE_FUNCTION_ENDPOINT']);
    res.json({
      'success': true,
    }, status: 200);
  } catch (e) {
    print(e);
  }
}
Drake
25 May, 2023, 02:28

You must call res.json() or res.send() exactly once before your function finishes.

Also, make sure to pass a string to print rather than an object

hortigado
25 May, 2023, 02:30

ok, but it doesn't print anything. It is as if the function does not start

Drake
25 May, 2023, 02:33

It's because your function never sent back a response so it timed out (and the output of print is in the response)

Binyamin
25 May, 2023, 02:43

You try can add this to get your error.

TypeScript
  } catch (e) {
    res.json({"e":e});
  }
hortigado
25 May, 2023, 02:52

Hello again, I create a new function and paste the same code and for some reason it works

Drake
25 May, 2023, 02:52

Did you create the variables?

hortigado
25 May, 2023, 02:54

Now I'm going to try this but for the moment it already returns a successful response

hortigado
25 May, 2023, 03:38
TypeScript
  final client = Client();
  final teams = Teams(client);

  if (req.variables['APPWRITE_FUNCTION_ENDPOINT'] == null ||
      req.variables['APPWRITE_FUNCTION_API_KEY'] == null) {
    res.json({
      'error': "Environment variables are not set. Function cannot use Appwrite SDK.",
    });

  } else {
    try {
      client
          .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
          .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
          .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
          .setSelfSigned(status: true);
      final payload = jsonDecode(req.payload == '' ? '{}' : req.payload);
      final String teamId = payload['teamId'];
      final String userTo = payload['userTo'];
      await teams.createMembership(
          teamId: teamId,
          email: userTo,
          roles: [],
          url: req.variables['APPWRITE_FUNCTION_ENDPOINT']);
      res.json({
        'success': true,
      });
    } catch (e) {
      res.json({
        'Error': e.toString(),
      });
    }
  }
}
hortigado
25 May, 2023, 03:39

Thank you for all, problem solved, I did not understand how to send an response. Creating a function I managed to understand. I attach the code with which I get it to work for me

hortigado
25 May, 2023, 03:41

[SOLVED] Function is not executed

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more