Back

[SOLVED] Error with Python SDK database.get_document

  • 0
  • Databases
  • Functions
thet3o
11 Aug, 2023, 21:16

I'm trying to get data from the database but I've always a generic runtime error, I've tried with php code but I don't find where I'm wrong

The code is on the screenshot

TL;DR
The user had an error with the Python SDK `database.get_document` function. After making a few changes to their code, they were able to successfully get the desired result. The solution involved printing the error as a string and adding a try/except block to catch exceptions. The user also posted their updated code for reference.
Drake
11 Aug, 2023, 21:59

would you please try to wrap your code with a try/except to catch any exception?

thet3o
11 Aug, 2023, 21:59

Tried but when I test I got only runtime error

Drake
11 Aug, 2023, 21:59

what's your updated code?

Drake
11 Aug, 2023, 22:00

copying and pasting here would be easier

thet3o
11 Aug, 2023, 22:09

thank you for waiting

thet3o
11 Aug, 2023, 22:09

import os import json

from appwrite.client import Client from appwrite.services.databases import Databases from appwrite.services.users import Users from appwrite.id import ID from appwrite.query import Query

def main(req, res): payload = json.loads(req.variables.get('APPWRITE_FUNCTION_EVENT_DATA')) client = Client() client.set_endpoint(req.variables.get("APPWRITE_ENDPOINT")) client.set_project(req.variables.get("APPWRITE_PROJECT_ID")) client.set_key(req.variables.get("APPWRITE_API_KEY"))

TypeScript
#Get Users collection id
database_id = req.variables.get('DATABASE_ID')
users_collection_id = req.variables.get('USERS_COLLECTION_ID')
database = Databases(client)
users = Users(client)
db_result = users.get(payload['userId'])

try:
    result = database.get_document(database_id, users_collection_id, payload['userId'])

    if result is None:
        database.create_document(
            database_id, 
            users_collection_id, 
            ID.custom(id=payload['userId']),
            data={
                'userId': payload['userId'],
                'name': db_result['name'],
                'email': db_result["email"],
        })
except Exception as e:
    print(e)
Drake
11 Aug, 2023, 22:16

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting.

Drake
11 Aug, 2023, 22:17

Would you please make sure res.json() or res.send() is called exactly once before exiting?

thet3o
11 Aug, 2023, 22:18
TypeScript

import os
import json

from appwrite.client import Client
from appwrite.services.databases import Databases
from appwrite.services.users import Users
from appwrite.id import ID
from appwrite.query import Query

def main(req, res):
    payload = json.loads(req.variables.get('APPWRITE_FUNCTION_EVENT_DATA'))
    client = Client()
    client.set_endpoint(req.variables.get("APPWRITE_ENDPOINT"))
    client.set_project(req.variables.get("APPWRITE_PROJECT_ID"))
    client.set_key(req.variables.get("APPWRITE_API_KEY"))
    
    #Get Users collection id
    database_id = req.variables.get('DATABASE_ID')
    users_collection_id = req.variables.get('USERS_COLLECTION_ID')
    database = Databases(client)
    users = Users(client)
    db_result = users.get(payload['userId'])
    
    try:
        result = database.get_document(database_id, users_collection_id, payload['userId'])
    
        if result is None:
            database.create_document(
                database_id, 
                users_collection_id, 
                ID.custom(id=payload['userId']),
                data={
                    'userId': payload['userId'],
                    'name': db_result['name'],
                    'email': db_result["email"],
            })
    except Exception as e:
        print(e)
thet3o
11 Aug, 2023, 22:20

Thank you I didn't remember it

Drake
12 Aug, 2023, 00:13

did that solve your problem?

thet3o
12 Aug, 2023, 00:14

Nope, in that message I'm referring to markdown😂

thet3o
12 Aug, 2023, 00:16

I tried again with same code but I hadn't any logs, nothing, I can't find where is the problem in the code

Drake
12 Aug, 2023, 00:17

what's your updated code?

Drake
12 Aug, 2023, 00:17

also, try to only print strings so: print(str(e))

thet3o
12 Aug, 2023, 00:20

Nothing, now because the try catch it says completed but no response are recorded

Drake
12 Aug, 2023, 00:21

what's your updated code?

thet3o
12 Aug, 2023, 00:22

Same only with the change from print(e) to print(str(e))

Drake
12 Aug, 2023, 00:25

Would you please make sure res.json() or res.send() is called exactly once before exiting?

thet3o
12 Aug, 2023, 00:25

res.json

thet3o
12 Aug, 2023, 00:26

Now with the same exactly code, I got the result

thet3o
12 Aug, 2023, 00:27

Maybe I miswrite a character, idk

thet3o
12 Aug, 2023, 00:27

Thank you for help!

Drake
12 Aug, 2023, 00:32

[SOLVED] Error with Python SDK database.get_document

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