Rank 1: Thread
The client is setup with
client.setEndpoint('https://cloud.appwrite.io/v1')
but I haven't set an endpoint for the server, I thought this was automatically set (I'm using Appwrite cloud)
Votes
0
Replies
24
Participants
Unknown
Messages
25
Rank 1: Thread
The client is setup with
client.setEndpoint('https://cloud.appwrite.io/v1')
but I haven't set an endpoint for the server, I thought this was automatically set (I'm using Appwrite cloud)
It's okay, it can be confused at start
In your function settings page you should set this values
APPWRITE_FUNCTION_ENDPOINT
APPWRITE_FUNCTION_PROJECT_ID
APPWRITE_FUNCTION_API_KEY
Rank 1: Thread
Ah ok, no mine are empty. If I set these, won't they be overwritten each time I deploy the function from the CLI?
No
This part is not related to deploy
Rank 1: Thread
Ok great I'll try that, thanks for your help @Binyamin
Rank 1: Thread
Sorry one more quick question @Binyamin am I passing data correctly between the client and server using JSON.stringify() and JSON.parse() above?
Yes, this is the right way to hanlde function data.
Rank 1: Thread
Great, thank you

Admin
You should be using req.payload for the input data rather than the event data variable. The event data variable is for executions triggered by events
Rank 1: Thread
Thanks @Steven I'll change to
const payload = JSON.parse(req.payload)
What is the different between APPWRITE_FUNCTION_DATA and req.payload
It looks like they both holds the same information don't they?
https://github.com/appwrite/appwrite/blob/master/app/controllers/api/functions.php#L1152
https://github.com/appwrite/appwrite/blob/master/app/executor.php#L507
Rank 1: Thread
I made the changes above, deployed and got the same error. Then I tried the 'Execute Now' button in the console with some mock JSON data, and got a different error:
SyntaxError: Unexpected token t in JSON at position 5 at JSON.parse (<anonymous>) at module.exports (/usr/code-start/src/index.js:21:24) at /usr/local/src/server.js:68:19 at processTicksAndRejections (node:internal/process/task_queues:96:5)req {"variables":{"APPWRITE_FUNCTION_ENDPOINT":"https://cloud.appwrite.io/v1","APPWRITE_FUNCTION_PROJECT_ID":"removedforprivacy","APPWRITE_FUNCTION_API_KEY":"removedforprivacy","APPWRITE_FUNCTION_ID":"647687110d37ce2558c1","APPWRITE_FUNCTION_NAME":"setupTeam","APPWRITE_FUNCTION_DEPLOYMENT":"6476b848e8e486567ea9","APPWRITE_FUNCTION_TRIGGER":"http","APPWRITE_FUNCTION_RUNTIME_NAME":"Node.js","APPWRITE_FUNCTION_RUNTIME_VERSION":"16.0","APPWRITE_FUNCTION_EVENT":"","APPWRITE_FUNCTION_EVENT_DATA":"","APPWRITE_FUNCTION_DATA":"{\n teamId: \"123d3984ur\", \n teamName:\"Demo Team\"\n}","APPWRITE_FUNCTION_USER_ID":"6475dab3e1dd9253ae62","APPWRITE_FUNCTION_JWT":"removedforprivacy","INERNAL_EXECUTOR_HOSTNAME":"exc1"},"headers":{},"payload":"{\n teamId: \"123d3984ur\", \n teamName:\"Demo Team\"\n}"}res {}Admin
req.payload is when the create execution API is called. Event data is for executions triggered by events: https://github.com/appwrite/appwrite/blob/bbc9bb823df1a4afb5d83baf96a1a25e57d279a5/app/workers/functions.php#L299
Admin
Your mock data is not valid JSON
Rank 1: Thread
Ok, I carefully changed the keys/values on the demo data, but I will try again and validate the JSON this time
Admin
JSON keys should be wrapped with double quotes
Rank 1: Thread
The JSON provided in the sample is not valid. I tried again with valid JSON and back to the internal curl error
Not in both?
It's seems like req.payload is just a placeholder for $data
Admin
I'm pretty sure this was fixed in the latest version of Appwrite
Rank 1: Thread
I'm using Cloud so not sure about versions. Anyway the internal curl error is thrown with valid JSON payload, and all the variables updated, so not sure what it can be. I wish we could get a better error message to narrow it down.
If you try to execute in this way
Try this
{"teamId":"123d3984ur","teamName":"Demo Team"}Or this
{teamId:"123d3984ur",teamName:"Demo Team"}Admin
Try wrapping all your code in a try catch and make sure res.json is called exactly once
To make things simpler, you should also convert the then to await
Rank 1: Thread
Ok thanks @Steven and @Binyamin . The databases are being created when I execute from the console, but the execution status is error. I'll refactor the function to catch better errors, but for now I have to go to work. Thanks for your help so far.