Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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 节点js:Express+;加载网页时,ssh2不会重置我的数据结构_Javascript_Node.js_Express - Fatal编程技术网

Javascript 节点js:Express+;加载网页时,ssh2不会重置我的数据结构

Javascript 节点js:Express+;加载网页时,ssh2不会重置我的数据结构,javascript,node.js,express,Javascript,Node.js,Express,要显示用户发送到集群的作业,我有以下代码(简化): 它在接收数据后打开ssh连接,并呈现页面 问题:仅在第一次正确执行时,在我们的示例console.log中显示: [Head_1',[Head_2'],[1,2]好的,问题解决了。我发这篇文章只是为了防止其他人有类似的问题,这可能会给我们一些启示 这是由于我的Javascript糟糕的编码实践。如果将函数renderable中的所有内容都包含在内,那么它就可以工作,这是零(0)个全局变量 内部的所有函数、所有变量(重要:包括'require()

要显示用户发送到集群的作业,我有以下代码(简化):

它在接收数据后打开ssh连接,并呈现页面

问题:仅在第一次正确执行时,在我们的示例console.log中显示:


[Head_1',[Head_2'],[1,2]好的,问题解决了。我发这篇文章只是为了防止其他人有类似的问题,这可能会给我们一些启示

这是由于我的Javascript糟糕的编码实践。如果将函数renderable中的所有内容都包含在内,那么它就可以工作,这是零(0)个全局变量

内部的所有函数、所有变量(重要:包括'require()'s):

module.exports={
可渲染:函数(req、res){

我猜表在全局范围内,因此您会看到问题。请执行以下操作:tableMap[“userName”]={},其中{}是您当前使用的表对象。您也可以使用闭包来解决此问题。您是对的,Michael,全局范围内的变量导致了问题。
var split = require('split');
var Client = require('ssh2').Client;
var conn = new Client();

var globalRes;
var table = [["Head_1","Head_2"]];

module.exports = {
    renderTable: function(req, res) {

         conn.connect({host: 'xx.xx.xx.xx', port: 22, username: 'xxxxx', password: 'xxxxx'});

         globalRes = res;
         table = [["Head_1","Head_2"]];
         conn.on('ready', function() { conn.shell(doSomething);} );
    }
}

function doSomething(err, stream) {

    stream.on('close', function() {
         conn.end();
         globalRes.render('index', { HTMLtable: table });
         console.log(table);
    } );
    stream.pipe(split()).on('data', buildTable);
    stream.write('qstat -s z\n');
    stream.end('exit\n');
}

function buildTable(line) {
    var newLine = [1, 2];
    if(line.substring(0,6) == "job-ID") {
         table.push(newLine);
    }
    return;
}
module.exports = {
    renderTable: function(req, res) {

           <<<< Put everything here.

    }
}