javascript 如何解决节点js中的ReferenceError(未定义var)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22451086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:11:32  来源:igfitidea点击:

how to solve a ReferenceError in node js (var not defined)

javascriptnode.jssockjs

提问by ederollora

I'm new to javascript to I'm probably asking something easy. I'm trying to develop a project using SockJS, nodeJs and ssh2 module of nodeJs.

我是 javascript 新手,我可能会问一些简单的问题。我正在尝试使用 nodeJs 的 SockJS、nodeJs 和 ssh2 模块开发一个项目。

I'm trying to send a message from Chrome using SockJS library's client side code (websockets) to the NodeJS server and then from the NodeJs to my raspberry pi via ssh2 module. The problem is the following one:

我正在尝试使用 SockJS 库的客户端代码(websockets)将消息从 Chrome 发送到 NodeJS 服务器,然后通过 ssh2 模块从 NodeJs 发送到我的树莓派。问题是以下一个:

var http = require('http');
var sockjs = require('sockjs');
var Connection = require('ssh2');

var c = new Connection();
var sockserver = sockjs.createServer();


var server = http.createServer();
sockserver.installHandlers(server, {prefix:'/socksvr'});
server.listen(9999, '0.0.0.0');

sockserver.on('connection', function(conn) {

    c.connect({
        // Parameters to connect
    });

    //Here goes some code to check that the connection was successful

    conn.on('data', function(message) { 
    //this is when Node receives data from client socket

        //print somethings
        c.shell(onShell); // This is to send command from node to Raspberry Pi

    });

    //Here goes the problem 

    var onShell = function(err, stream) { 
        if (err != null) {
            console.log('error: ' + err);
        }

        stream.on('data', function(data, extended) {
            if (extended === 'stderr'){
                console.log('STDERR: '+data);
                conn.write(data);
            }else{
                if(data != ''){
                    console.log(':' + data);
                    conn.write(':' + data);
                }
            };        
        });

        stream.write(message);

    }

}); 


\server.js:62
stream.write(message);
             ^
ReferenceError: message is not defined
at onShell (C:\Documents and Settings\...\server.js:62:18)
at C:\Documents and Settings\...\node_modules\ssh2\lib\Channel.js:277:5
at Parser.<anonymous> (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Channel.js:123:30)
at Parser.EventEmitter.emit (events.js:92:17)
at Parser.parsePacket (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Parser.js:594:12)
at Parser.execute (C:\Documents and Settings\...\node_modules\ssh2\lib\P
arser.js:238:14)
at Socket.<anonymous> (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Connection.js:488:18)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)

I knew that using messagevar inside that function wasn't probably going to work but I haven't been able to figure out the solution. I'm probably lacking some knowledge about functions. Any help is appreciated. Thank you.

我知道在该函数中使用messagevar 可能不会起作用,但我一直无法找出解决方案。我可能缺乏一些关于函数的知识。任何帮助表示赞赏。谢谢你。

回答by user3100381

Your message variable only exists inside the scope of the conn.on('data', function. You would have to declare message in sockserver.on('connection', or the scope above for it to be visible in both functions.

您的消息变量仅存在于 conn.on('data', 函数的范围内。您必须在 sockserver.on('connection' 或上面的范围内声明消息才能在两个函数中都可见。

回答by Louis

You are trying to access messageout of scope. You could do this to pass it to onShell:

您正在尝试访问message超出范围。您可以这样做以将其传递给onShell

conn.on('data', function(message) { 
    c.shell(function (err, stream) {
        onShell(err, stream, message);
    });
});

var onShell = function(err, stream, message) { 
// Etc...

A function's arguments are only accessible insidethe function.

函数的参数只能函数内部访问。