字符串变为未定义

时间:2014-04-07 17:40:18

标签: javascript node.js

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
                    ...

如代码中所述,var消息是一个字符串,然后,我不知道为什么,它变成一个未定义的变量。为什么会这样?我没有将它分配给另一个值。

2 个答案:

答案 0 :(得分:2)

根据执行上下文,function执行的bot.whois可能没有在范围中定义message。您可以通过传递message

来使用闭包来确保范围
(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);

答案 1 :(得分:0)

您的代码显然是不完整的,并且实际的错误可能位于您的截止点以下某处,因为您提出了问题:

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

您需要确保没有弄乱whois电话下面的消息,或者您需要创建一个不会弄乱的副本,或者您需要关注@ Romoku的建议并将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
    (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

请注意我的示例和@ Romoku的message已经明确重命名(分别为msgm),以明确您正在使用在不同的范围内使用不同的数据副本。