Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 字符串变得未定义 bot.addListener('message',函数(from,channel,message){ write(“[”+(new Date()).toJSON()+”][“+channel+”]“+message+”\n”); //===================播放器命令=========================// 如果(日志消息){ util.log(“[”+通道+“]”+消息); } console.log(message)//记录消息 bot.whois(from,function(whois){ 如果(WHOIS.account的类型==“未定义”){ var isAuthed=false; }否则{ var isAuthed=true; } 如果(message.indexOf(“!”===0){//则消息未定义 ..._Javascript_Node.js - Fatal编程技术网

Javascript 字符串变得未定义 bot.addListener('message',函数(from,channel,message){ write(“[”+(new Date()).toJSON()+”][“+channel+”]“+message+”\n”); //===================播放器命令=========================// 如果(日志消息){ util.log(“[”+通道+“]”+消息); } console.log(message)//记录消息 bot.whois(from,function(whois){ 如果(WHOIS.account的类型==“未定义”){ var isAuthed=false; }否则{ var isAuthed=true; } 如果(message.indexOf(“!”===0){//则消息未定义 ...

Javascript 字符串变得未定义 bot.addListener('message',函数(from,channel,message){ write(“[”+(new Date()).toJSON()+”][“+channel+”]“+message+”\n”); //===================播放器命令=========================// 如果(日志消息){ util.log(“[”+通道+“]”+消息); } console.log(message)//记录消息 bot.whois(from,function(whois){ 如果(WHOIS.account的类型==“未定义”){ var isAuthed=false; }否则{ var isAuthed=true; } 如果(message.indexOf(“!”===0){//则消息未定义 ...,javascript,node.js,Javascript,Node.js,正如代码中所描述的,var消息是一个字符串,然后,我不知道为什么,它变成了一个未定义的变量。为什么会发生这种情况?我没有将它赋给另一个值。根据执行上下文,bot.whois执行的函数可能没有在范围中定义消息通过传递消息,使用闭包来确保作用域 bot.addListener('message', function (from, channel, message) { IRClog.write("[" + (new Date()).toJSON() + "] [" + channel + "

正如代码中所描述的,var消息是一个字符串,然后,我不知道为什么,它变成了一个未定义的变量。为什么会发生这种情况?我没有将它赋给另一个值。

根据执行上下文,
bot.whois
执行的
函数可能没有在范围中定义
消息通过传递
消息
,使用闭包来确保作用域

bot.addListener('message', function (from, channel, message) {
    IRClog.write("[" + (new Date()).toJSON() + "] [" + channel + "] <" + from + ">" + message + "\n");
    // ============= PLAYER COMMANDS ============= //
    if (logMessages){
        util.log("[" + channel + "] <" + from + ">" + message);
    }
    console.log(message)// the message is logged
    bot.whois(from,function(WHOIS){
        if(typeof WHOIS.account == 'undefined'){
            var isAuthed = false;
        } else {
            var isAuthed = true;
        }
        if (message.indexOf("!") === 0){//now the message is undefined
                    ...

很明显,您的代码是不完整的,实际的bug可能位于您在问题中提出的截止点以下:

(function (msg) {
    console.log(msg)// the message is logged
    bot.whois(from, function(WHOIS){
        var isAuthed = typeof WHOIS.account !== 'undefined';
        if (msg.indexOf("!") === 0) {
            ...
        }
})(message);

请注意,在我的示例和@Romoku's中,
消息
是如何被明确重命名的(分别为
msg
m
),以明确您正在使用不同的数据副本在不同的作用域中工作。

JavaScript具有动态作用域,因此当执行
bot.whois
时,
消息
变量可能不会在作用域上下文中定义。解决方案是传入
消息
,而不是取决于正在执行的变量在作用域中。我怎么做?你可以使用闭包。不,如果
bot.whois
处理程序函数是在
bot.addListener
处理程序函数中创建的,那么
message
将在作用域中。除非你覆盖它,否则无法阻止它。事实上,
message
已经被关闭了,就像你的uggest by作为第一行的参数传递给外部函数。不起作用。尝试使用:console.log(msg);(函数(消息){console.log(消息);bot.whois(from,函数(whois){console.log(消息);Logs:string,string,undefined您可以将其编辑到您的问题中。拥有一个更完整的代码示例也会有所帮助。@AlexWayne
message
在范围内,但它不是本地的,因此可以从外部范围(似乎是这样)更改它。闭包应该纠正这一点,尽管这不是唯一的方法。
bot.addListener('message', function (from, channel, message) {
    IRClog.write("[" + (new Date()).toJSON() + "] [" + channel + "] <" + from + ">" + message + "\n");
    // ============= PLAYER COMMANDS ============= //
    if (logMessages){
        util.log("[" + channel + "] <" + from + ">" + message);
    }
    console.log(message)// the message is logged
    bot.whois(from,function(WHOIS){
        if(typeof WHOIS.account == 'undefined'){
            var isAuthed = false;
        } else {
            var isAuthed = true;
        }
        if (message.indexOf("!") === 0){//now the message is undefined
                    ...
    }); // end of the whois block, which is asynchronous

    /* somewhere down here, message probably gets set to undefined
    like this:
    message = undefined; // this would run before bot.whois(from, cb); finishes
    */
}); // end of addListener
bot.addListener('message', function (from, channel, message) {
    IRClog.write("[" + (new Date()).toJSON() + "] [" + channel + "] <" + from + ">" + message + "\n");
    // ============= PLAYER COMMANDS ============= //
    if (logMessages){
        util.log("[" + channel + "] <" + from + ">" + message);
    }
    console.log(message)// the message is logged
    (function (msg) {
        bot.whois(from,function(WHOIS){
            if(typeof WHOIS.account == 'undefined'){
                var isAuthed = false;
            } else {
                var isAuthed = true;
            }
            if (msg.indexOf("!") === 0){//now the message is undefined
                        ...
        }); // end of the whois block, which is asynchronous
    })(message);

    /* now you can do whatever you want to the message variable and bot.whois will
    be able to operate on its independent, unmolested copy
    */
}); // end of addListener