1

im new to node.js and programming in itself. I am trying to call a function in anther programm but i never get the result. The function I am calling works fine, the result is correct and calling the function works fine too, but the array I want to return just doesnt get returned. The program runs without errors (and sadly without output on the console). I checked several tutorials but cannot find a difference to what I am doing. I tried putting the "return(result);" at the end of the function (before closing the last "}") but by doing so the array is empty again. I appreciate every idea.

regards, Nils

1. prog: calling the function:

var input = require('./einlesen.js');
var output = input.einlesen('GZV.csv', ';', 1, 2, function(err, yay)
{   
console.log(output);
});

2. prog: the function:

    var einlesen = function(datei, trennzeichen, zelle1, zelle2) 
    {
    var result = [];                                            //Ergebnis array beinhaltet start und endknoten

    var fs = require('fs'),                                     //filestream einbinden um fs.open etc zu nutzen
    data = 'Daten/' + datei;

    fs.open(data, 'r', function(err, handledata)                //zu prüfende Datendatei einbinden
    {
        fs.stat(data, function(err,datastats)                   //dateiinformationen über größe auslesen
        {
            var datasize = datastats.size,
            databuffer = new Buffer(datasize);
            var dataarray = [];

            fs.read(handledata, databuffer, 0, datasize, 0, function(err, bytes, datacontent)       //Datei zum lesen öffnen
            {
                var datalines = datacontent.toString().split("\n");                                 //array mit Zeilen der Datei füllen

                for (var i=0; i < datalines.length - 1; i += 1)                                     //Zeile für Zeile die Daten bearbeiten
                {
                    dataarray[i] = datalines[i].split(trennzeichen);                                //Zeile aufsplitten
                    dataarray [i][zelle1] = dataarray[i][zelle1].trim();                            //Leerezeichen entfernen
                    dataarray [i][zelle1] = dataarray[i][zelle1].trim('\r');                        // \r abschneiden
                    if (zelle2 != 'n')
                    {
                        dataarray [i][zelle2] = dataarray[i][zelle2].trim();                        //Leerzeichen entfernen
                        dataarray [i][zelle2] = dataarray[i][zelle2].trim('\r');                    // \r abschneiden
                    }
                    result[i] = [dataarray[i][zelle1], dataarray[i][zelle2]];
                }
            fs.close(handledata);
            });
        });
    });
    }
    exports.einlesen = einlesen;
1
  • You don't have any callback called... Commented May 4, 2014 at 11:07

2 Answers 2

1

The result is not passed back to the caller (using callback). Here is the change:

prog: calling the function:

var input = require('./einlesen.js');
input.einlesen('GZV.csv', ';', 1, 2, function(err, output)
{   
    console.log(output);
});

einlesen.js:

NOTE: last argument "callback" is added to this function. Also, callback() is called at the end to return the result

var einlesen = function(datei, trennzeichen, zelle1, zelle2, callback) {
    var result = [];                                            //Ergebnis array beinhaltet start und endknoten

    var fs = require('fs'),                                     //filestream einbinden um fs.open etc zu nutzen
    data = 'Daten/' + datei;

    fs.open(data, 'r', function(err, handledata, callback) {
        fs.stat(data, function(err,datastats) {
            var datasize = datastats.size,
            databuffer = new Buffer(datasize);
            var dataarray = [];

            fs.read(handledata, databuffer, 0, datasize, 0, function(err, bytes, datacontent) {
                var datalines = datacontent.toString().split("\n");                                 //array mit Zeilen der Datei füllen

                for (var i=0; i < datalines.length - 1; i += 1) {
                    dataarray[i] = datalines[i].split(trennzeichen);                                //Zeile aufsplitten
                    dataarray [i][zelle1] = dataarray[i][zelle1].trim();                            //Leerezeichen entfernen
                    dataarray [i][zelle1] = dataarray[i][zelle1].trim('\r');                        // \r abschneiden
                    if (zelle2 != 'n') {
                        dataarray [i][zelle2] = dataarray[i][zelle2].trim();                        //Leerzeichen entfernen
                        dataarray [i][zelle2] = dataarray[i][zelle2].trim('\r');                    // \r abschneiden
                    }
                    result[i] = [dataarray[i][zelle1], dataarray[i][zelle2]];
                }
                fs.close(handledata);
                callback(null, result);   // 1st argument: null means no error, 2nd argument is the reuslt
            });
        });
    });
};
exports.einlesen = einlesen;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you as well Ben. Now I can finally go on with the real program :)
0

You have to add a callback function to your einlesen function and call that function once your finished with reading and constructing the result array.

var einlesen = function(datei, trennzeichen, zelle1, zelle2, cb) // Callback function
{
var result = [];                                            //Ergebnis array beinhaltet start und endknoten

var fs = require('fs'),                                     //filestream einbinden um fs.open etc zu nutzen
data = 'Daten/' + datei;

fs.open(data, 'r', function(err, handledata)                //zu prüfende Datendatei einbinden
{
    if (err) { return cb(err); }                            // Ensure that in case of an error we do not proceed
    fs.stat(data, function(err,datastats)                   //dateiinformationen über größe auslesen
    {
        if (err) { return cb(err); }                        // Ensure that in case of an error we do not proceed
        var datasize = datastats.size,
        databuffer = new Buffer(datasize);
        var dataarray = [];

        fs.read(handledata, databuffer, 0, datasize, 0, function(err, bytes, datacontent)       //Datei zum lesen öffnen
        {
            // Code removed for clarity...
            fs.close(handledata);

            cb(undefined, result); // Now call the callback
        });
    });
});
}

Some code fragments that were used to parse file data and build the result were removed for clarity. This code adds two "exit points" where the code returns in case an error was passed to a callback handler.

You might also consider using a more simple approach to read all data from a file. fs.readFile and fs.readFileSync allow you to read all file contents text in one operation.

1 Comment

Hi Saintedlama, thx alot, and sorry for the late reply, i thought i would be noticed if anyone answered my question :) along with Ben's changes below everything works fine now. I will surely try some other methods for reading data, but first I needed this to work. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.