Back

[SOLVED] Appwrite Cloud function not working

  • 0
  • Functions
  • Flutter
Prateek Jain
27 Apr, 2023, 09:42

I have created a cloud function but it is not working on appwrite cloud. It should trigger on the event - User successfully verified phone number To do - create a new user document in database. My code -

`import json from appwrite.client import Client from appwrite.services.databases import Databases

def main(req, res): client = Client()

TypeScript
(client
  .set_endpoint('https://cloud.appwrite.io/v1')
  .set_project('[MY_PROJECT_ID]')
  .set_key('[MY_KEY]')
)
databases = Databases(client)
payload = json.loads(req.payload)
result = databases.create_document('[MY_DATABASE_ID]', '[MY_COLLECTION_ID]', ID.unique(), {
    'name': payload['name'],
    'mobileNo': payload['phone'],
    'uid': payload['uid'],
},)
res.json({result})

`

TL;DR
Appwrite Cloud function not working. The issue was due to not accessing the event data correctly. By using `APPWRITE_FUNCTION_EVENT_DATA` variable, you can retrieve the event data and avoid exceptions. Additionally, make sure your function does not throw any exceptions and that you call `res.json` or `res.send` before returning. If you encounter an error between the proxy and executor, check your function deployments and execution logs for any potential issues. Also, ensure that you have set the necessary variables in your Appwrite project, including the Database ID, Project ID, and API Key. Double-check the video tutorial for more guidance.
anyuru
27 Apr, 2023, 10:00

Did you set the variables in your Appwrite project. Like the Database Id, Project Id, and API Key? You can check out the video. Something like that.

anyuru
27 Apr, 2023, 10:03

Additionally you can also review your function deployments and execution logs to confirm if the function was successfully deployed or if there is any error in the function execution

Prateek Jain
27 Apr, 2023, 10:08

in the logs it is showing error as Unexpected curl error between proxy and executor: Connection reset by peer

D5
27 Apr, 2023, 12:40

You are using appwrite cloud beta, true? Or appwrite self-host/localhost?

Prateek Jain
27 Apr, 2023, 12:40

appwrite cloud beta

Drake
27 Apr, 2023, 13:07

For functions triggered by event, the payload will be an empty string and your function will throw an exception.

In general, make sure your function never throws an exception and make sure to call res.json or res.send exactly once before returning

Prateek Jain
27 Apr, 2023, 14:33

thank u very much sir... but then how to get the payload sir as soon as the user is created?

Binyamin
27 Apr, 2023, 15:00

You'll need to check a second variable named APPWRITE_FUNCTION_EVENT_DATA this one will contain all the event data, something like this

TypeScript
try{
  final event = json.loads(req.variables["APPWRITE_FUNCTION_EVENT_DATA"])
} catch(e){
  // handle error
}
Prateek Jain
27 Apr, 2023, 18:12

Thank you all for the help

Prateek Jain
28 Apr, 2023, 08:38

[SOLVED] Appwrite Cloud function not working

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