【问题标题】:node.js callback in a for-loopfor循环中的node.js回调
【发布时间】:2014-06-09 12:58:05
【问题描述】:

我试图读取一些数据并将其放入一个多数组中。要打开不同的文件,我使用 for 循环,因为我需要以正确的顺序对结果进行编号。我的问题:

我无法将数据保存在一个数组中。一旦我调用函数读取数据,我只能在函数边界内使用结果。如何使整个数组在此函数之外可用? (很确定我搞砸了回调)。

换句话说:我正在尝试打开多个文件并将内容保存在一个数组 [[file1]、[file2]、...] 中。

这是我的代码:

var einlesen = require ('./einlesen/einlesen.js');

var einzeln = function(arg, callback){   //this is the function I use to read the data (it works fine, I checked it separately)
    einlesen.einlesen(arg[0], arg[1], arg[2], function(err, output){  //this function opens a file, works on the content and returns an array containing the needed data
        if (err) { return cb1(err); }
        callback(output);
    });
}

var daten_einlesen = function (callback){

    var tabellenarray = []; //initiating the array in which the whole info should go
    var tabelleninfos = [   //this array contains the info which data should be read, you only need to know that there are 16 files to be read, the rest is not important
                            ['gvz', '<|>', '1,1,1,0,1,0,0,0,1'], 
                            ['GZV', ';', '1,1,1,1,1,1,1,1'], 
                            ['XPG', ';', '1,1,1,1,1'], 
                            ['ABF', ';', '1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1'], 
                            ['GUS', '<|>', '1,1,0,1,0,0,0,0,1,1,1'], 
                            ['XPA', '<|>', '1,1,1'], 
                            ['ALW', '<|>', '1,1,1'], 
                            ['TAL', '<|>', '1,1,1'], 
                            ['ALB', '<|>', '1,1,0,1,1,1,1,1,1'], 
                            ['ZLB', '<|>', '1,1,1'], 
                            ['XAW', '<|>', '1,1,1'], 
                            ['SWS', ';', '1,1,1,1,1,1,1,1,1,1'], 
                            ['PRO', '<|>', '1,1'], 
                            ['VBP', '<|>', '1,0,1'], 
                            ['ZVP', '<|>', '1,1,1'],
                            ['config', ';', '0,0,1']];

    for (var i = 0; i < 3; i++){  //this is the loop where i try to open the files one by one and input them into the "tabellenarray"
        einzeln(tabelleninfos[i], function(tabelle){
            tabellenarray[i] = tabelle; //data is saved to the array but I lose the info onto the next step of the loop, if i put the final callback (below) in here the loop doesnt work anymore (naturally)
        });
    }       
    callback(tabellenarray);
}

daten_einlesen(function(piep){ //calling the whole function
console.log(piep);
});

感谢您的任何建议。

尼尔斯

【问题讨论】:

  • 哪个数组?以及您必须访问哪个功能之外??
  • 我想我可以帮助你,但是einlesen.einlesen() 是什么?它有什么作用?另外 - 你在寻找什么输出?
  • 让我们看看...函数“einlesen”打开一个 .csv 文件,对数据进行一些处理并返回一个数组。然后这个数组应该被保存到一个多数组“tabellenarray”中,所以最后“tabellenarray”对于每个参数都有一个数组,其中包含用“einlesen”打开的不同文件的数据。那就是我尝试使用for循环的地方,它为“tabelleninfos”中的每个项目调用函数“einlesen”,并且应该将返回的数据保存到“tabellenarray”中。在整个函数的最后,我想返回“tabellenarray”,这样我就可以处理所有数据了。
  • 我认为因为 einzeln 异步执行您可能过早调用 callback(tabellenarray) 的数据,这往往是将异步代码与副作用结合起来的问题。未决的标志或承诺可能会有所帮助。我会回答这个效果。

标签: node.js loops for-loop callback


【解决方案1】:

初学者试试这个:

var pending = 3,complete=0;
for (var i = 0; i < pending; i++){  //this is the loop where i try to open the files one by one    and input them into the "tabellenarray"
    einzeln(tabelleninfos[i], function(tabelle){
        tabellenarray[i] = tabelle; //data is saved to the array but I lose the info onto the next step of the loop, if i put the final callback (below) in here the loop doesnt work anymore (naturally)
        if(++complete >= pending){
            callback(tabellenarray);
        }
    });
}       

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多