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
- Need help with createExecution function
Hi, Need some help understanding createExecution. When requesting function execution via createExecution, the function handler arguments are incorrect and rese...
- Apple OAuth Scopes
Hi Hi, I've configured sign in with apple and this is the response i'm getting from apple once i've signed in. I cant find anywhere I set scopes. I remember se...
- Sign In With Apple OAuth Help
Hi All! I've got a flutter & appwrite app which Im trying to use sign in with apple for. I already have sign in with google working and the function is the sam...