Rank 3: Container
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.
Votes
0
Replies
20
Participants
Unknown
Messages
21
Rank 3: Container
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.
Admin
This usually happens because there's an exception thrown in your code.
Did you start with the start template created by the Appwrite CLI?
Rank 3: Container
`Future 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);
}
`
Rank 3: Container
yes I edited it a bit, that's how I have it now
Rank 3: Container
Admin
Make sure to use 3 back ticks instead of 1 for multi line code
Admin
I suggest wrapping everything in a big try/catch to make sure there are no uncaught exceptions
Admin
That res.json also looks incorrect
Rank 3: Container
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
Admin
I don't know what you mean
Rank 3: Container
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); }}Admin
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
Rank 3: Container
ok, but it doesn't print anything. It is as if the function does not start
Admin
It's because your function never sent back a response so it timed out (and the output of print is in the response)
You try can add this to get your error.
} catch (e) { res.json({"e":e}); }Rank 3: Container
Hello again, I create a new function and paste the same code and for some reason it works
Admin
Did you create the variables?
Rank 3: Container
Now I'm going to try this but for the moment it already returns a successful response
Rank 3: Container
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(), }); } }}Rank 3: Container
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
Rank 3: Container
[SOLVED] Function is not executed