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
would you please try to wrap your code with a try/except to catch any exception?
Tried but when I test I got only runtime error
what's your updated code?
copying and pasting here would be easier
thank you for waiting
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)
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.
Would you please make sure res.json() or res.send() is called exactly once before exiting?
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)
Thank you I didn't remember it
did that solve your problem?
Nope, in that message I'm referring to markdownπ
I tried again with same code but I hadn't any logs, nothing, I can't find where is the problem in the code
what's your updated code?
also, try to only print strings so: print(str(e))
Nothing, now because the try catch it says completed but no response are recorded
what's your updated code?
Same only with the change from print(e) to print(str(e))
Would you please make sure res.json() or res.send() is called exactly once before exiting?
res.json
Now with the same exactly code, I got the result
Maybe I miswrite a character, idk
Thank you for help!
[SOLVED] Error with Python SDK database.get_document
Recommended threads
- Fulltext index creation fails due to Inn...
I'm running a self-hosted Appwrite 1.8.1 instance and encountering an issue when creating fulltext indexes on a collection. **The Problem:** Fulltext index cre...
- Function Building show 500 Error
Region: sfo Temple: Starter Function Runtime: Nodejs 22 What happened? Before 2 minutes it show βserver error.","code":500,"version":"0.7.24"}β after it show c...
- Cannot Update Empty oneToMany Relationsh...
Appwrite's `updateDocument()` API cannot update an empty oneToMany relationship array. When attempting to update an empty relationship with a single-item array,...