Back

Getting appwrite.exception.AppwriteException: Invalid document structure: for even for valid request

  • 0
  • Databases
Vedsaga
4 May, 2023, 08:41

I am trying to create document

TypeScript
        requestBody = {
            "districtName": row['districtName'],
            "pincode": row['pincode'],
        }
        print(f"requestBody--> {requestBody}")
        result = databases.create_document(
            databaseId,
            collectionId,
            ID.unique(),
            requestBody
        )

and request which is printed in the terminal is

TypeScript
requestBody--> {'districtName': 'SOUTH EAST DELHI', 'pincode': 110003}

I don't see any issue with request body then why this error? Can anyone pls help point if I am doing something dumb things or have missed something ?

TL;DR
The user is encountering an invalid document structure error when trying to create a document using the Appwrite API. They are passing a pincode value of 110003, which they believe is valid. However, the error message indicates that the pincode should be a valid range between 6 and 6, not the number of digits. The user realizes their mistake and retracts their assumption. They also mention that inserting multiple documents at once is not currently possible. Another user provides a code snippet for creating documents concurrently using the Appwrite client and database objects. The user successfully creates 18,000 documents using this method.
Drake
4 May, 2023, 15:55

What's the full error and what's your collection?

Vedsaga
4 May, 2023, 16:10
TypeScript

Traceback (most recent call last):
  File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\appwrite\client.py", line 97, in call
    response.raise_for_status()
  File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.bitecope.com/v1/databases/6451d90b58e5630094e5/collections/64535aec379151d13abf/documents

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\MVP\appwrite\dump_pincode_in_appwrite.py", line 30, in <module>
    result = databases.create_document(
  File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\appwrite\services\databases.py", line 967, in create_document
    return self.client.call('post', path, {
  File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-p  File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\appwrite\services\databases.py", line 967, in create_document    return self.client.call('post', path, {
  File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\appwrite\client.py", line 109, in call
    raise AppwriteException(response.json()['message'], response.status_code, response.json().get('type'), response.json())
appwrite.exception.AppwriteException: Invalid document structure: Attribute "pincode" has invalid format. Value must be a valid range between 6 and 6
Vedsaga
4 May, 2023, 16:10

this is kinda full traceback

Drake
4 May, 2023, 16:15

Value must be a valid range between 6 and 6

That's the problem...

Drake
4 May, 2023, 16:15

pincode should be a number between 6 and 6. it doesn't mean the number of digits

Vedsaga
4 May, 2023, 16:15

yaa, I don't know why it's happening requestBody--> {'districtName': 'SOUTH EAST DELHI', 'pincode': 110003} here in print it does have valid

Drake
4 May, 2023, 16:16

no that is not valid

Vedsaga
4 May, 2023, 16:17

oh wait does it mean 6 like literal 6 ? because I thought 6 mean.. len πŸ‘€

Drake
4 May, 2023, 16:17

yes, literal 6

Vedsaga
4 May, 2023, 16:17

πŸ˜† oh no

Vedsaga
4 May, 2023, 16:18

such a silly thing I thought it works like len that being counted

Vedsaga
4 May, 2023, 16:18

I guess will re-try and confim here

Vedsaga
4 May, 2023, 16:57

is it possible to do insert all?

Vedsaga
4 May, 2023, 17:05

just saw that it's not possible at this moment in time https://github.com/appwrite/appwrite/issues/3051

Vedsaga
4 May, 2023, 17:21

for now I did try creating concurrently... Incase anyone want to know the code or for me for future refrence...

Vedsaga
4 May, 2023, 17:22
TypeScript
from appwrite.client import Client
from appwrite.services.databases import Databases
from appwrite.id import ID
import json
import concurrent.futures



endpoint = '<ENDPOINT>'


# Replace <PROJECT_ID> with your Appwrite project ID
project_id = '<PROJECT_ID>'

# Replace <API_KEY> with your Appwrite API key
api_key = '<API_KEY>'

# Set the <DATABASE_ID> for the Appwrite database
database_id = '<DATABASE_ID>'

# Set the <COLLECTION_ID> for the Appwrite documents
collection_id = '<COLLECTION_ID>'
# Initialize the Appwrite client

client = Client().set_endpoint(endpoint).set_project(project_id).set_key(api_key)
databases = Databases(client)

# Create an Appwrite client and database object
client = Client()
client.set_endpoint(endpoint)
client.set_project(project_id)
client.set_key(api_key)
database = Databases(client)

# Define a function to create a single document in Appwrite
def create_document(document):
    result = database.create_document(database_id, collection_id, ID.unique(), document)
    return result['$id']

# Load the JSON data from the input file
with open('pincode_unique.json', 'r') as input_file:
    json_data = json.load(input_file)

    # Create a thread pool to create the documents concurrently
    with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
        # Submit each dictionary in the JSON data to the thread pool
        futures = [executor.submit(create_document, document) for document in json_data]

        # Print the results of each completed future
        for future in concurrent.futures.as_completed(futures):
            print(future.result())
Vedsaga
4 May, 2023, 17:23

created 18 thoushand document in couple of mins

Vedsaga
4 May, 2023, 17:24

I mean 18K πŸ˜…

Vedsaga
4 May, 2023, 17:29

Fun-fact I didn't wrote this code not even a single line, I asked https://codeium.com/

It's free VS-code extension if incase anyone what to use πŸ‘€ quite facinating it would have taken be hours to fig this out

conqueror
7 May, 2023, 13:11

can i get the same code in flutter as not familiar with js

Vedsaga
7 May, 2023, 13:11

yes, sure... it's one line switch

Vedsaga
7 May, 2023, 13:12

I assume you do have toJson function already...

conqueror
7 May, 2023, 13:13

no i dont have, i was jsut thinking about this json fnction that how i can make

Vedsaga
7 May, 2023, 13:14
TypeScript

Future<List<T>> executeConcurrently<T>(List<Future<T>> futures) async {
  final results = <T>[];

  for (final future in futures) {
    try {
      final result = await future;
      results.add(result);
    } catch (e) {
      print('Error executing future: $e');
    }
  }

  return results;
}
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