变量不在函数内部访问

Variable not accessing inside of function

我为我的 twitch 机器人制作了一个功能。当他们输入命令时检查他们是否已经在列表中。如果他们不在列表中,它将把它们添加到列表中。下面的代码输出这个:[ undefined ] [17:53] info: [#kong_plays] *<kng_bot>: 1

var wtpLength = [];
function lfg(user){
    WTPLength = viewersWTP.length;
    if (WTPLength !== 0) {
        viewersWTP = viewersWTP - 1
        while(WTPLength !== -1){
            if(user === viewersWTP[WTPLength]){
                console.log("Already in list")
                client.action("kong_plays",user+" you cannot execute this command again until kong pulls a viewer to play with him!")
            }else{
            viewersWTP.push(user['display-name'])
            console.log("Added into list")
            client.action("kong_plays",viewersWTP)
            }
        }
    }else{
        viewersWTP.push(user['display-name'])
            console.log(viewersWTP)
            client.action("kong_plays","1"+viewersWTP)
    }
}

类似的方法可能对您有用。您正在使用未定义的变量。看起来您似乎也没有注意正在定义的变量的情况。

var viewersWTP = [];   // add this or define it elsewhere
function lfg(user){
    var wtpLength = viewersWTP.length;
    if (wtpLength !== 0) {
        viewersWTP = viewersWTP - 1
        while(wtpLength !== -1){
            if(user === viewersWTP[wtpLength]){
                console.log("Already in list")
                client.action("kong_plays",user+" you cannot execute this command again until kong pulls a viewer to play with him!")
            }else{
            viewersWTP.push(user['display-name'])
            console.log("Added into list")
            client.action("kong_plays",viewersWTP)
            }
        }
    }else{
        viewersWTP.push(user['display-name'])
            console.log(viewersWTP)
            client.action("kong_plays","1"+viewersWTP)
    }
}

user['display-name']改为user。您通过函数

传递了该变量