#First function help

1 messages · Page 1 of 1 (latest)

fresh current
#

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']

# 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'])

untold bronze
#

To easily create a function for Appwrite

#

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

#

In your code, its looks like client side code

fresh current
#

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

untold bronze
#

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

fresh current
untold bronze
#

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

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
        }
    )
#

This is the basic code

#

It won't work out of the box