Skip to content

🐛 Bug Report: Cannot update document from a functions #8794

@Zia-Ch

Description

@Zia-Ch

👟 Reproduction steps

Hey Appwrite team 👋,

I’m working on a Python function.
this function 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] Error saving updated credits: The document data and permissions are missing. You must provide either document data or permissions to be updated.

Reproduction steps:
-- Create a simple python function with provided code.
-- Create a User_Collection that has only one attribute credits (int).
-- create a document and put some value here
-- run your function

##Code

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


def 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)
    
      

What I’ve Tried:

  1. Ensured that data is a dictionary with the credits field.
  2. Attempted to pass permissions correctly.
  3. Printed and verified the data and permissions before calling update_document
    Despite these efforts, the error persists. 🥲

👍 Expected behavior

Document will get updated and document should have the updated values

👎 Actual Behavior

It is not saving data instead showing error:
[ERROR] Error saving updated credits: The document data and permissions are missing. You must provide either document data or permissions to be updated.

🎲 Appwrite version

Different version (specify in environment)

💻 Operating system

Linux

🧱 Your Environment

Appwrite 1.6.0
Linux server with Ubuntu 22.04
python runtime 3.11
using dynamic API from functions that has all database access.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingproduct / databasesFixes and upgrades for the Appwrite Database.product / functionsFixes and upgrades for the Appwrite Functions.product / self-hostedIssues only found when self-hosting Appwrite

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions