Rank 2: Process
Hey Appwrite team!
I develop a Python function.
it first get data from user collection, decrease their credits and try to update it.
Apparently it should work fine but unexpectedly it is failing in updating the document.
It throws this error:
[ERROR] The document data and permissions are missing. You must provide either document data or permissions to be updated.
##Code
from appwrite.client import Clientfrom appwrite.services.databases import Databasesdef main(context): #get required values from headers user_id = context.req.headers['x-appwrite-user-id'] api_key=context.req.headers['x-appwrite-key'] #get required values from environment variables endpoint=os.environ['APPWRITE_FUNCTION_API_ENDPOINT'] project_id=os.environ['APPWRITE_FUNCTION_PROJECT_ID'] db_id=os.environ['DB_ID'] user_collection_id=os.environ['USER_COLLECTION_ID'] #setup appwrite client client = Client() client.set_endpoint(endpoint) client.set_project(project_id) client.set_key(api_key) database = Databases(client) #get user details try: user = database.get_document(db_id, user_collection_id, user_id) context.log(f'User details: {user}') except Exception as e: context.log(f'Error getting user details: {e}') #reduce user credits by 1 updated_credits = user['credits'] - 1 context.log(f'Updated credits: {updated_credits}') try: #save updated credits to user document save = database.update_document( database_id=db_id, collection_id=user_collection_id, document_id= user_id, data={'credits': updated_credits} ) context.log(f'saved: {save}') return context.res.json({'ok': True,'credits': updated_credits},200) except Exception as e: context.log(f'Error saving updated credits: {e}') return context.res.json({'ok': False,'error': str(e)},400)