Back

[SOLVED] Updating an attribute in an event for the updated document?

  • 0
  • Self Hosted
  • Functions
ZachHandley
2 Nov, 2023, 00:44

Just curious, I have an event on every update for my databases that basically checks if something is a searchable document (they have the attribute search) and then it puts all the searchable attributes into a JSON array of the searchable stuff and is supposed to update it (so they are always kept up to date automatically) but that is not seemingly working

TypeScript
def main(context):
    # Why not try the Appwrite SDK?
    client = (
        Client()
        .set_endpoint("https://appwrite.pva.ai/v1")
        .set_project(os.environ["APPWRITE_FUNCTION_PROJECT_ID"])
        .set_key(os.environ["APPWRITE_API_KEY"])
        .set_self_signed(True)
    )
    database = Databases(client)
    context.log("UpdateSearchColumns function called")
    document = context.req.body
    database_id = document.get("$databaseId")
    collection_id = document.get("$collectionId")
    document_id = document.get("$id")
    searchable = {}
    if collection_id in searchable_collections:
        # Get keys of dict and compare to searchable_attributes
        # then take all searchable attributes, combine them into one object, and update the document with search object as a JSON string version
        # of the attributes in one object
        searchable = {}
        for key in document.keys():
            if key in searchable_attributes and document.get("key", None) is not None:
                searchable[key] = document[key]
        searchable = json.dumps(searchable)
        if document.get("search", None) != searchable:
            context.log(f"Updated Document: {json.dumps(document, indent=4)}")
            database.update_document(database_id, collection_id, document_id, { "search": searchable })
    return context.res.empty()

It should work, and it says updated, but the document has no text in it's search attribute

TL;DR
The user is experiencing an issue with updating an attribute in an event for a document. They are trying to update the 'search' attribute of a searchable document, but it is not working. The provided code shows that the 'search' attribute is being updated with a JSON string of searchable attributes. The issue seems to be with the line `document.get("key", None)`. The user should be using `document.get(key, None)` instead. Solution: Replace `document.get("key", None)` with `document.get(key, None)` in the code.
ZachHandley
2 Nov, 2023, 00:57

Nevermind, I was trying to get document.get("key") instead of key

ZachHandley
2 Nov, 2023, 00:57

[SOLVED] Updating an attribute in an event for the updated 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