繁体   English   中英

对于我的示例,如何处理 python 中的 KeyError

[英]How can I handle KeyError in python for my example

我正在使用 Python 和 Firebase 制作电报机器人。 我被困在一个 KeyError 上:

2021-02-12 12:39:38,687 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\dispatcher.py", line 442, in process_update
    handler.handle_update(update, self, check, context)
  File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\conversationhandler.py", line 549, in handle_update
    new_state = handler.handle_update(update, dispatcher, check_result, context)
  File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\handler.py", line 160, in handle_update
    return self.callback(update, context)
  File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\bot.py", line 61, in start
    print(user.val()["chat_id"])
KeyError: 'chat_id'

这是我的代码:

def start(update: Update, context: CallbackContext) -> int:
    global PauseKey, PauseStart, PauseState, PauseAllKey
    timenow = datetime.now()
    auth = firebase.auth()
    userx = auth.sign_in_with_email_and_password('drux@mail.com', '12345678')
    db = firebase.database()
    chat_user_client = update.message.from_user.username
    all_users = db.child("users").get()
    user_status = "none"

    for user in all_users.each():
        print(user.val()["chat_id"])
        if user.val()["chat_id"] == update.message.chat_id:
            user_status = "exist"
            userKey = user.key()
            break
    print(user_status)
    if user_status == "exist":
        text_back = "You was logged in, Welcome back "
    else:
        data = {"name": chat_user_client, "chat_id": update.message.chat_id}
        date = {"date": datetime.today().strftime('%Y-%m-%d-%H:%M:%S')}
        userKey= db.child("users").push(data)
        userKey = userKey.val()
        text_back = "User was create"


    update.message.reply_text(
    f'{text_back}{chat_user_client}',
    reply_markup=markup2
    )


    return CHOOSING

错误块:

for user in all_users.each():
        print(user.val()["chat_id"])
        if user.val()["chat_id"] == update.message.chat_id:
            user_status = "exist"
            userKey = user.key()
            break

并且此打印print(user.val()["chat_id"])在控制台中显示690953230 这是一个正确的聊天 ID,存放在我的 firebase 中。

对于这一行,我尝试将 await 与 asyncio 库一起使用:

all_users = db.child("users").get()
async def start()
...
all_user = await db.child("users").get()
...

随着异步和等待错误离开,但代码没有做预期的事情。

代码应执行以下操作:浏览所有用户数据并检查 user_chat_id 是否已在数据库中。 如果 chat_id 已经在数据库中,则用户存在并且我正在使用他的表键。 如果用户不存在,我正在创建他的表并获取他的用户密钥。

我被这个错误困住了几个小时,无法自己处理,请帮忙。

Edit

现在我发现如果我的用户存在,代码工作正常。 我的意思是,如果我的 chat_id 已经在数据库中,我不会收到任何错误,如果我从数据库中删除我的数据我得到 keyError

我建议您在从字典中获取元素时使用 get; 因为虽然 dict["key"] 给出错误,但 dict.get("key") 在密钥不存在时不会给出错误。 我的建议是将错误块更新为:

for user in all_users.each():
    chat_id = user.val().get("chat_id")
    if chat_id and  chat_id == update.message.chat_id:
        user_status = "exist"
        userKey = user.key()
        break

此外,您应该进行一些重构以增强可读性。 例如,在上面的代码块中,下面的赋值看起来是多余的:

userKey = user.key()

您还可以提取用户存在检查作为另一种方法:

def user_exists(all_users, msg_chat_id):
    for user in all_users.each():
        chat_id = user.val().get("chat_id")
        if chat_id and  chat_id == msg_chat_id:
            return true
    return false

然后在 start 方法中使用它:

def start(update: Update, context: CallbackContext) -> int:
    global PauseKey, PauseStart, PauseState, PauseAllKey
    timenow = datetime.now()
    auth = firebase.auth()
    userx = auth.sign_in_with_email_and_password('drux@mail.com', '12345678')
    db = firebase.database()
    chat_user_client = update.message.from_user.username
    all_users = db.child("users").get()

    if user_exists(all_users, update.message.chat_id):
        text_back = "You was logged in, Welcome back "
    else:
        data = {"name": chat_user_client, "chat_id": update.message.chat_id}
        date = {"date": datetime.today().strftime('%Y-%m-%d-%H:%M:%S')}
        userKey= db.child("users").push(data)
        userKey = userKey.val()
        text_back = "User was create"


    update.message.reply_text(
    f'{text_back}{chat_user_client}',
    reply_markup=markup2
    )


    return CHOOSING

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM