Rank 2: Process
Why not always use the same payload structure, to streamline execution, and not confuse new users by referencing things in the Docs that don't fully correlate to the variable names? 🙂
Votes
0
Replies
24
Participants
Unknown
Messages
25
Rank 2: Process
Why not always use the same payload structure, to streamline execution, and not confuse new users by referencing things in the Docs that don't fully correlate to the variable names? 🙂
Rank 2: Process
Is there a reason why the payload data for executions could not be stored in the same variable as the payload data for an event?
Rank 2: Process
module.exports = (req, res) => { const maybePayload = req.payload; const maybeAlsoPayload = req.variables.APPWRITE_FUNCTION_EVENT_DATA const trigger = req.variables.APPWRITE_FUNCTION_TRIGGER;
let actualPayload;
if (trigger === 'http') { actualPayload = maybePayload; } if (trigger === 'event') { actualPayload = maybeAlsoPayload }
// do things with payload}Rank 2: Process
Unnecessary boilerplate imho 👆 - unless there is some technical limitation to having all payloads normalized.
Just seems a bit odd to me that the variable is stored in different names. The main confusion was about the docs mentioning "payload" for all events, and then req.payload being empty when called from an event. You understand I'm sure.
Moderator
Supposedly when you're triggering an execution you will not need payload
Moderator
The payload functionality is to send data from a client to a function (as you might know)
Moderator
So if you're scheduling execution, the data sent to it through payload is not going to change, consequently it's practically useless, since for static data you have env variables
Rank 2: Process
I know all this. I understand how it all works! I'm just questioning, or rather asking for, the reasoning behind doubling up on the variable inputs, and/or trying to make a case for making this clearer in the documentation.
Rank 2: Process
It's because I know how it works that I'm questioning it.
Rank 2: Process
You can see why it would make sense, especially when testing Functions, to be able to trigger a Function
made to respond to events, but using an execution with a payload (for testing, or various other reasons).
Rank 2: Process
But in order to do that we have to consider that the payload can be in multiple places.
Rank 2: Process
I might add this to my toolkit.
const { getPayload, getClient, CollectionHelper} = require('x')
module.exports = (req, res) => { const payload = getPayload(req) // normalize payload entry, and return json const client = getClient(req) // build a client from req.variables const databases = new Databases(client)
const userId = req.variables.APPWRITE_FUNCTION_USER_ID;
// forwards CRUD operations so we don't have to define which database and collection each time const messages = new CollectionHelper(databases, 'chat', 'messages')
if (payload.owner !== userId) { messages.update(payload.$id, { owner: userId }) }
res.json({success: true})}good question...I would assume it's to make it clear that req.variables.APPWRITE_FUNCTION_EVENT_DATA is data that came from an event and req.payload is input payload. I'll ask the team for additional insight, though
referencing things in the Docs that don't fully correlate to the variable names
What do you mean?
The main confusion was about the docs mentioning "payload" for all events
Can you provide more details about this? I found 1 reference in the function variables docs (https://appwrite.io/docs/functions#functionVariables). Where else?
Rank 2: Process
This was my journey as someone who has not touched Appwrite before:
payload of the event, greatappwrite init functionreq.payload right there in the file, neat!req.payload... then why is my event payload empty?APPWRITE_FUNCTION_EVENT_DATA, wait, it's not payload? Why is it not payload.Rank 2: Process
It could just be that the words get mixed up. Payload is of course a generic term, and it is the right word, in the context where there is a variable literally named "payload" however mixups can happen. There is no mention, nor explanation for why event data is separate from execution data. I don't see why it would be. This is nitpicky, it just caused me some frustation, and I'm sure I'm not the only one who goes through the same.
interesting...thanks for the detailed insight! 
ya..."payload" is generic and we use it in multiple places, including realtime 👀
Rank 2: Process
It's all good, sorry if I'm being shouty, I'm just nitpicking - Appwrite is great
no worries! this is really great feedback! i've passed it along to the team so we can discuss this and how to improve
ok so in the next version of functions, we won't have the data in multiple places. it'll only be in req.body. hopefully that will be simple/clear enough.
Moderator
req.body? What's that? 😅
Moderator
Isn't req.data?
In next version we focus on making functions follow HTTP standards. So on request you will have stuff like body, headers, url, method...
If I am not mistaken, all that was considered payload until now will go into request body. So no matter if manual execution, event, or anything else.. Incoming data will always be in req.body