Back

First function help

  • 0
  • Functions
ski
13 Jul, 2023, 03:34

Hey team sorry for the unnecessary support req but this is the first time am write a cloud function as I start to learn python I just want to know if this function is correct if not can someone help on what I should change.

from appwrite.client import Client from appwrite.services.database import Database

//Initialize the Appwrite client client = Client() client.set_endpoint('https://YOUR_APPWRITE_ENDPOINT') client.set_project('YOUR_APPWRITE_PROJECT') client.set_key('YOUR_APPWRITE_API_KEY')

//Initialize the database service database = Database(client)

//Define the function to auto-populate profile collection def populate_profile(document): //Extract the necessary fields from the user's payload userId = document['$id'] email = document['email'] name = document['name']

TypeScript
# Create a new document in the profile collection
profileDocument = database.create_document(
    collectionId='YOUR_PROFILE_COLLECTION_ID',
    data={
        'userId': userId,
        'email': email,
        'name': name,
        # Add additional fields as needed
    }
)

//Log the newly created profile document ID
print('Profile document created:', profileDocument['$id'])

//Call the function when a user is created populate_profile(event['payload'])

TL;DR
The user is asking for help with their first cloud function using Appwrite. They are unsure if their code is correct and would like recommendations on what to change. Another user suggests using the Appwrite cli or starting with one of the function starters available on GitHub. The code provided by the user is outdated and won't work, so the recommended solution is to use the updated code provided in the support thread. The updated code includes setting the Appwrite project, key, collection ID, and database ID. It also includes initializing the Appwrite client and database service, defining the function to auto-populate the profile collection, and calling the function when
Binyamin
13 Jul, 2023, 03:35

To easily create a function for Appwrite

Binyamin
13 Jul, 2023, 03:35

You can either use the Appwrite cli - which is the best way.

Binyamin
13 Jul, 2023, 03:35

Or you can copy and start with one of the functions starters available here https://github.com/appwrite/functions-starter/tree/main/python-3.10

Binyamin
13 Jul, 2023, 03:36

In your code, its looks like client side code

ski
13 Jul, 2023, 03:36

I sure will btw since am new to python what would you recommend to change in my function

Binyamin
13 Jul, 2023, 03:37

Are you referring to Appwrite cloud functions, Or, in general?

Drake
13 Jul, 2023, 03:38

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

ski
13 Jul, 2023, 03:41

Appwrite cloud function

Binyamin
13 Jul, 2023, 03:44

So for that you'll need to put your code in the starter function, like the link above. It will look something like this

TypeScript
from appwrite.client import Client
from appwrite.services.databases import Databases

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

  client.set_endpoint('https://your_appwrite_endpoint/')
  client.set_project('YOUR_APPWRITE_PROJECT')
  client.set_key('YOUR_APPWRITE_API_KEY')

  database = Databases(client)
  populate_profile(req.payload) 


  return res.json({
    "areDevelopersAwesome": True,
  })

def populate_profile(document):
    userId = document['$id']
    email = document['email']
    name = document['name']

    # Create a new document in the profile collection
    profileDocument = database.create_document(
        collectionId='YOUR_PROFILE_COLLECTION_ID',
        data={
            'userId': userId,
            'email': email,
            'name': name,
            # Add additional fields as needed
        }
    )
Binyamin
13 Jul, 2023, 03:44

This is the basic code

Binyamin
13 Jul, 2023, 03:44

It won't work out of the box

Binyamin
13 Jul, 2023, 03:46

You'll need to set, the

  • Appwrite project
  • Appwrite key
  • CollectionID
  • DatabaseID

The code you have used seems to be outdated. As it use an older version of the create_document function without database.

What I recommend is to try this:

ski
13 Jul, 2023, 03:48

I sure will check them out, thank you for your help!

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