
I am writing an app with flutter. On user.create : a function is triggered to create a document. I understand with users.create trigger, the whole "user" object is passed as payload to the function. So I guess it means I can get all the info about user I need within the function. I am faced with syntax problem and just can't figure it out with the docs. (I am a beginner).
Can someone help me understand how : I connect the function to the cloud project (I see different syntaxes in different examples), then how I actually get the "payload" with the user object : what is the syntax to get the info... I am a bit lost.... So here is the code I used so far. My question : where and how do get the info about the user ? import 'dart:io'; import 'dart:async'; import 'package:dart_appwrite/dart_appwrite.dart';
Future<dynamic> main(final context) async { final client = Client() .setEndpoint('https://cloud.appwrite.io/v1') .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID']) .setKey(Platform.environment['APPWRITE_API_KEY']);
final databases = Databases(client);
try {
await databases.createDocument(
databaseId: '[DATABASE_ID]',
collectionId: '[COLLECTION_ID]',
documentId: ID.unique(),
data: {}
);
} catch (e) {
context.error("Failed to create document: " + e.message);
return context.res.send("Failed to create document");
}
return context.res.send("Document created");
}

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting.

They payload will be in context.req.body
.
https://appwrite.io/docs/products/functions/development#request

Thank you for your help. I have tried this : var userData=json.encode(context.req.body); context.log(userData.toString()); context.log(userData); context.log(userData["$id"]); The first one works. But the one where I want to just get the user ID doesn't work.... A syntax problem I am sure... Can you help ?

It's most likely due to the json encode.
Instead of userData=json.encode(context.req.body);
try userData=jcontext.req.body;
.

Actually, just realised you're not escaping the $
symbol it should be ["\$id"]

Thanks for your help. It works.

It was simpler than I thought....

Yeah the core attributes are always with $ in front of them, like the created and modified date.

How to get function event payload?

[SOLVED] How to get function event payload?
Recommended threads
- How to get event payload/source from fai...
Hi everyone, I'm currently working with Appwrite's database triggers, specifically for document creation events. I've encountered a potential issue and was h...
- Creating a custom function ID in the con...
Console Version 1.8.0 Google chrome Version 139.0.7258.66 (Official Build) (64-bit)
- Unable to create string element with co...
Wrong string created in createdocument in collection with String array attribute Array string like this ["aa,bb","cc,dd"] is created with 4 elements instead of ...
