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

javascript - Nodejs readline callback - Stack Overflow

programmeradmin11浏览0评论

I am studying callbacks, and for some reason I can't get it right... I want to read a file, and save it's data to a global variable to play with later.

Here is what I have so far:

var fs = require("fs");
var readline = require("readline");
var i = 0;
var total = 66; //put the total foldernames or total images (same number)
var folder_names = [];
var data = [];

lineReader = readline.createInterface({
    input: fs.createReadStream("folder-names and data.txt")
});


lineReader.on('line', function(line, dataCollector) {
    if(i<66)
        folder_names.push(line);
    else
        data.push(line);

    dataCollector(folder_names, data);
    i++;
});

var dataCollector = function(folder_names, data) {
    //console.log(folder_names);
}

console.log(folder_names[0]); //should have a value now.

What is wrong? I get: dataCollector is not a function

I am studying callbacks, and for some reason I can't get it right... I want to read a file, and save it's data to a global variable to play with later.

Here is what I have so far:

var fs = require("fs");
var readline = require("readline");
var i = 0;
var total = 66; //put the total foldernames or total images (same number)
var folder_names = [];
var data = [];

lineReader = readline.createInterface({
    input: fs.createReadStream("folder-names and data.txt")
});


lineReader.on('line', function(line, dataCollector) {
    if(i<66)
        folder_names.push(line);
    else
        data.push(line);

    dataCollector(folder_names, data);
    i++;
});

var dataCollector = function(folder_names, data) {
    //console.log(folder_names);
}

console.log(folder_names[0]); //should have a value now.

What is wrong? I get: dataCollector is not a function

Share Improve this question edited Sep 30, 2016 at 11:43 Florin Pop asked Sep 30, 2016 at 11:35 Florin PopFlorin Pop 5,1354 gold badges28 silver badges60 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You're shadowing the dataCollector identifier here:

lineReader.on('line', function(line, dataCollector) {

That declares dataCollector as the second parameter to your callback, shadowing (hiding) the identifier at the top level of your script.

The line event doesn't document that it provides a second argument to its callback, so it should look like this:

lineReader.on('line', function(line) {

Re your extension of the question:

console.log(folder_names[0]); //should have a value now.

No, it shouldn't. Why: How do I return the response from an asynchronous call?

In your case, you probably want to do your console.log in an close event handler:

lineReader
    .on('line', function(line) {
        if(i<66)
            folder_names.push(line);
        else
            data.push(line);

        dataCollector(folder_names, data);
        i++;
    })
    .on('close', function() {
        console.log(folder_names[0]); // has its values now
    });

You declare your function using var which will be done when the line is reached. So when you call it in your callback, the function has not been defined yet. To be able to use it, either move it before lineReader.on('line', function(){}) or (better) define it like that:

function dataCollector(folder_names, data) {
  /* Your function */
}

Doing it this way, your function is declared before your script is executed, and thus it exists when you reach your callback.

发布评论

评论列表(0)

  1. 暂无评论