【问题标题】:TypeError: object NoneType can't be used in 'await' expression(bot)TypeError:对象 NoneType 不能用于“等待”表达式(机器人)
【发布时间】:2021-02-26 05:57:58
【问题描述】:

我正在编写一个可以向用户提问的机器人。我把答案放在我后来创建的类的对象中。在继续用户操作结束时,我会显示“发送答案”按钮。用户按下按钮,我将数据从对象传输到数据库。在那一刻我得到 type_error:

TypeError: object NoneType can't be used in 'await' expression

我检查我的对象 - 它不是空的。 我在这里添加我的代码:
主文件(我将用户数据传输到数据库的地方)
@dp.callback_query_handler(lambda c: c.data == 'send')
def send_data_to_base():
if not db.user_exist(storage.user_id) == False:
    db.add_new_user(storage.user_id, storage.gender, storage.age, storage.salary, storage.gov_rate, 
storage.city_condition, storage.level_of_development)

数据库代码:

import sqlite3

class SQLighter:
def __init__(self, database):
    self.connection = sqlite3.connect(database)
    self.cursor = self.connection.cursor()

def get_info(self):
    with self.connection:
        return self.cursor.execute("SELECT * FROM `answers`").fetchall()

def user_exist(self, user_id):
    with self.connection:
        result = self.cursor.execute("SELECT * FROM `answers` WHERE `user_id` = ?", (user_id,)).fetchall()
    return bool(len(result))

def add_new_user(self, user_id, gender, age, salary, gov_rate, city_condition, level_of_development):
    with self.connection:
        return self.cursor.execute("INSERT INTO `answers` (`user_id`, `gender`, `age`, `salary`, `gov_rate`, `city_condition`, `level_of_development`) VALUES(?,?,?,?,?,?,?)", (user_id, gender, age, salary, gov_rate, city_condition, level_of_development))

PS:我正在使用 aiogram 库。

【问题讨论】:

    标签: python sqlite bots telegram-bot


    【解决方案1】:

    在调用 add_new_user 函数时,我必须使我的函数 async 并使用 await

    @dp.callback_query_handler(text='send')
    async def send_data_to_base(query: CallbackQuery):
        ... 
        if not db.user_exist(storage.user_id):
            await db.add_new_user(storage.user_id, storage.gender, storage.age, storage.salary, storage.gov_rate, storage.city_condition, storage.level_of_development)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 2021-06-02
      • 2021-10-09
      相关资源
      最近更新 更多