TypeError: 'AsyncIOMotorCursor' object is not an iterator

I’ve tried using the following:

async for x in cursor - this may work but since the list is empty it's providing a different error than above relating to the cursor never being properly reassigned to a document/dictionary
for x in next(cursor):
filter = {'messageLink': '1148770911412043838:1234829780566806609:1234829811604783164'}

and all have yielding the above error

I have confirmed the query is in the DB by pasting the outputted query into MongoDB Compass and I’m able to locate the document.

Please ignore the indentation but here is the relevant code.

filter = {
            'messageLink': f'{payload.guild_id}:{payload.channel_id}:{payload.message_id}'
        cursor = roles_db.find(filter)

        if cursor is None:
        to_list = await cursor.to_list(length=100)
        for x in next(cursor):
        # async for x in cursor:
            if x['emoji'] == payload.emoji.name:
                cursor = x

Query being ran:

{'messageLink': '1148770911412043838:1234829780566806609:1234829811604783164'}

I have no idea how it started working, but I came back to it today and tested it, and it started working. I printed out the value of to_list and noticed it was being converted to a lit so I replaced “next(cursor)” with “to_list” and it’s working just fine.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.