Back

Not able to add new documents

  • 0
  • Databases
  • Functions
mitsy0_0
31 May, 2023, 19:13

This is my python function, and I'm not sure why it fails when run it in console What am I doing wrong?

TypeScript
def main(req, res):
  payload = req.payload or 'No payload provided. Add custom data when executing function.'


  secretKey = req.variables.get(
    'API_KEY_HERE',
    'SECRET_KEY variable not found. You can set it in Function settings.'
  )

  client = Client()

  (client
    .set_endpoint('https://cloud.appwrite.io/v1') # Your API Endpoint
    .set_project('PROJECT_ID_HERE') # Your project ID
    .set_key('API_KEY_HERE') # Your secret API key
  )

  databases = Databases(client)

  result = databases.create_document('DATABASE_ID_HERE', 'COLLECTION_ID_HERE', 'ID.unique()', {
                'Name': 'user123',
                'Logo': 'https://images1-fabric.practo.com.png',
                'Title': 'title123',
                'Location': 'loc1'
            },)
  
  return res.json({
    'message': 'Hello from Appwrite!',
    'payload': payload,
    'result': result
  })
TL;DR
Title: Unable to add new documents Messages: - User is trying to execute code and encountering an error. - User tried putting the code in a try/catch block but still encountered an error. - User confirms that all the strings have been changed in the code. - User checked for errors in the Appwrite console but found nothing. - User shares the error they are getting. - User is unsure if the ID should be a string or not. - User shares their Python function and asks for help in identifying the issue. Solution: - Add `from appwrite.id import ID` at the top of the document. - Confirm
Binyamin
31 May, 2023, 19:14

What is the error? Also maybe the ID shouldn't be a string?

TypeScript
  result = databases.create_document('DATABASE_ID_HERE', 'COLLECTION_ID_HERE', ID.unique(), {
                'Name': 'user123',
                'Logo': 'https://images1-fabric.practo.com.png',
                'Title': 'title123',
                'Location': 'loc1'
            },)
mitsy0_0
31 May, 2023, 19:16

if its outside the string, it says "ID" is not defined

mitsy0_0
31 May, 2023, 19:17

also, in the docs it says string (assuming I'm not misinterpreting it)

Binyamin
31 May, 2023, 19:17

Add this at the top of your document

TypeScript
from appwrite.id import ID
Binyamin
31 May, 2023, 19:18

Interesting, What is the error you're getting?

mitsy0_0
31 May, 2023, 19:19

there's nothing in errors/ logs

mitsy0_0
31 May, 2023, 19:19

leme try that

mitsy0_0
31 May, 2023, 19:20

failed again😫

Binyamin
31 May, 2023, 19:21

For this also set the ID.unique() as a function?

mitsy0_0
31 May, 2023, 19:21

yeah, did that

Binyamin
31 May, 2023, 19:22

Ohh, I'm sorry How did you try to execute the function?

mitsy0_0
31 May, 2023, 19:22

um from the console

Binyamin
31 May, 2023, 19:23

In Appwrite or a terminal?

mitsy0_0
31 May, 2023, 19:23

appwrite

Binyamin
31 May, 2023, 19:24

Do you any information in any of these tabs?

mitsy0_0
31 May, 2023, 19:24

nope

mitsy0_0
31 May, 2023, 19:24

they just empty

mitsy0_0
31 May, 2023, 19:25
Binyamin
31 May, 2023, 19:27

Just want to be sure all the string ends with *_HERE was changed by you for the example, yes?

mitsy0_0
31 May, 2023, 19:27

yeah

Binyamin
31 May, 2023, 19:29

Okay, let's try to put all the code inside try/catch block to see what is the exception if there is one.

TypeScript
    try:
      ...your code
    except Exception as e:
        return res.json({"success": False, "message": str(e)})
Binyamin
31 May, 2023, 19:29

After running your code like such you'll be able to see the error message in the Response tab in the console.

mitsy0_0
31 May, 2023, 19:30

okay let me try this

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