变量无法在函数内部访问

时间:2018-06-19 16:57:43

标签: javascript node.js twitch

ive为我的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)
    }
}

2 个答案:

答案 0 :(得分:0)

类似的事情可能对您有用。您正在使用未定义的变量。看起来您好像并没有注意定义的变量的情况。

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)
    }
}

答案 1 :(得分:0)

user['display-name']更改为user。您通过函数传递了该变量