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
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
Ok great I'll try that, thanks for your help @Binyamin
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.
Great, thank you
<:appwriteupvote:899677724615016528>
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
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
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 {}
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
Your mock data is not valid JSON
Ok, I carefully changed the keys/values on the demo data, but I will try again and validate the JSON this time
JSON keys should be wrapped with double quotes
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
I'm pretty sure this was fixed in the latest version of Appwrite
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"}
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
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.
Recommended threads
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...
- Project in AppWrite Cloud doesn't allow ...
I have a collection where the data can't be opened. When I check the functions, there are three instances of a function still running that can't be deleted. The...
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...