Back

[SOLVED] How to get function event payload?

  • 0
  • Flutter
  • Functions
SylvainJack
22 Nov, 2023, 16:57

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']);

TypeScript
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");

}

TL;DR
The user wants to know how to access the payload in a function event. The solution is to use `context.req.body` to access the payload.
Drake
22 Nov, 2023, 18:02

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.

Drake
22 Nov, 2023, 18:04
SylvainJack
23 Nov, 2023, 11:07

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 ?

Ernest
23 Nov, 2023, 11:19

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

Ernest
23 Nov, 2023, 11:22

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

SylvainJack
23 Nov, 2023, 15:07

Thanks for your help. It works.

SylvainJack
23 Nov, 2023, 15:08

It was simpler than I thought....

fafa
23 Nov, 2023, 15:14

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

Drake
23 Nov, 2023, 15:31

How to get function event payload?

Drake
23 Nov, 2023, 15:32

[SOLVED] How to get function event payload?

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