最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to solve a ReferenceError in node js (var not defined) - Stack Overflow

programmeradmin3浏览0评论

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.

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:

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 mand 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 message var 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.

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.

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:

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 mand 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 message var 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.

Share Improve this question asked Mar 17, 2014 at 9:49 ederolloraederollora 1,1812 gold badges11 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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.

You are trying to access message out of scope. You could do this to pass it to 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 inside the function.

发布评论

评论列表(0)

  1. 暂无评论