Try make my code more asynchronous and get this eror:
find = (await loop.run_until_complete(a.finddb()[0])) TypeError: 'coroutine' object is not subscriptable
from telethon import TelegramClient, events, Button, utils, sync
import pymongo
from pymongo import TEXT
import re
import logging
import asyncio
class Search(): # search in mongodb
def __init__(self, search): # connect to mongoDB
self.search = search
self.myclient = pymongo.MongoClient(
"mongodb+srv://:@cluster0.ye4cx.mongodb.net/info?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE")
self.mydb = self.myclient["info"]
self.mycol = self.mydb["comics"]
async def searchdb(self): # finds all comics by request
self.mycol.create_index([('title', TEXT)], default_language='english')
self.find = self.mycol.find({"$text": {"$search": self.search}})
if self.find.count() == 0:
return 0
else:
return (self.find)
async def finddb(self): # search info for 1 comics
self.mycol.create_index([('title', TEXT)], default_language='english')
self.find = self.mycol.find({"title": self.search})
return (self.find)
@bot.on(events.NewMessage(pattern=r'(?<=|).*(?=|)')) # command for find comics info
async def find(event):
loop = asyncio.get_event_loop()
a = Search(event.text.replace("|", ""))
find = await loop.run_until_complete(a.finddb()[0])
await event.respond(f'**|{find.get("title")}|**\n\n**Статус перевода**: {find.get("status")}\n**Издатель**: {find.get("publisher")}\n\n**Жанр**: {find.get("genres")}\n**Описание**:\n{find.get("description")}', buttons=[[Button.inline('Ссылки на скачку', b'next')]])
I try to use motor, but have same problem, but why it doesn't work? With pymongo it work perfect
New trouble find = (await a.finddb())[0] TypeError: 'AsyncIOMotorCursor' object is not subscriptable
from telethon import TelegramClient, events, Button, utils, sync
import re
import logging
import motor.motor_asyncio
class Search(): # search in mongodb
def __init__(self, search): # connect to mongoDB
self.search = search
self.myclient = motor.motor_asyncio.AsyncIOMotorClient("mongodb+srv://login:[email protected]/info?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE")
self.mydb = self.myclient["info"]
self.mycol = self.mydb["comics"]
async def searchdb(self): # finds all comics by request
self.find = self.mycol.find({"$text": {"$search": self.search}})
print(self.find)
if self.find.count() == 0:
return 0
else:
return (self.find)
async def finddb(self): # search info for 1 comics
self.find = self.mycol.find({"title": self.search})
return (self.find)
@bot.on(events.NewMessage(pattern=r'(?<=|).*(?=|)')) # command for find comics info
async def find(event):
a = Search(event.text.replace("|", ""))
find = (await a.finddb())[0]
print(find)
await event.respond(f'**|{find.get("title")}|**\n\n**Статус перевода**: {find.get("status")}\n**Издатель**: {find.get("publisher")}\n\n**Жанр**: {find.get("genres")}\n**Описание**:\n{find.get("description")}', buttons=[[Button.inline('Ссылки на скачку', b'next')]])
a.finddb()returns acoroutineobject, which isn't something you can treat like a list by asking for its first item.