即使密钥存在也存在密钥错误-Flask socket.io

时间:2019-06-13 14:45:37

标签: javascript python flask socket.io

我正在为聊天应用程序编写客户端和服务器代码。当我从客户端发出并以json格式将一些数据发送到服务器时,即使字典中存在密钥,服务器也会抛出密钥错误。代码如下:

    document.querySelector('#channels').addEventListener('click',function(e){
            if(e.target && e.target.matches('.new-button')){
                document.querySelector('#conversation').innerHTML = "";

                if (e.target.dataset.isPrivate == "yes") {
                    localStorage.setItem('isPrivate', "yes");
                }

                else {
                    localStorage.setItem('isPrivate', "no");
                }


                const buttonName = e.target.innerHTML;
                const isItPrivate = localStorage.getItem('isPrivate');
                const me = localStorage.getItem('userName');

                localStorage.setItem('activeChannel', buttonName);
                const relevantData = {"channel name": buttonName, "isItPrivate": isItPrivate, "me": me};
                socket.emit('channel changed', {"someInfo": relevantData});
                console.log(relevantData);

                }
    });

,服务器中的代码如下:

@socketio.on("channel changed")


def loadChannelMessages(data):


privateOption = data["someInfo"]["isItPrivate"]
print(privateOption)

if privateOption == "yes":
    channelToLoad = data["someInfo"]["channel name"]




    for x in messages:
        if x["name"] == channelToLoad:

            messagesToDisplay = x["data"]
            if messagesToDisplay != []:
                print("messages to display are " + str(messagesToDisplay))
                print ("messages are" + str(messages))
                emit("loading messages", messagesToDisplay, broadcast = False)
                break

else:
    firstHalf = data["someInfo"]["channel name"]
    secondHalf = data["someInfo"]["me"]
    for x in privateChat:
        if firsthalf in x["channel"] and secondHalf in x["channel"]:
            messagesToDisplay = x["convo"]
            if messagesToDisplay != []:
                emit("loading messages", messagesToDisplay, room = request.sid)

但是当我启动应用程序时,弹出的错误是:

第102行,在loadChannelMessages中     privateOption = data [“ someInfo”] [“ isItPrivate”] KeyError:“ someInfo”

为什么会发生?

0 个答案:

没有答案