1

Am taking baby steps with node js. First task, read a delimited file line by line, and spit out the lines that contain the string "imputed".

#!/usr/bin/env node

var Csv = require("csv"),
    Fs = require("fs");

var foods = "/../data/sr26";


    Csv()
        .from.stream(
            Fs.createReadStream(__dirname + foods + "/SRC_CD.txt"),
            {"delimiter": "^", "quote": "~"}
        )
        .to(function(data, count) {
            console.log(data);
        })
        .transform(function(row, index, callback) {
            //if (row[1].search(/imputed/) != -1) {
                process.nextTick(function() {
                     callback(
                        null, 
                        row[1].substr(0, 15) + "… " + row[1].search(/imputed/) + "\n"
                    )
                });
            //}
        });

Prints out

Analytical or d… -1
Calculated or i… 14
Value manufactu… -1
Aggregated data… -1
Assumed zero… -1
Calculated from… -1
Calculated by m… -1
Aggregated data… -1
Manufacturer's … -1
Analytical data… -1

But uncommenting the comparison inside transform results in no output at all. What am I doing wrong?

1 Answer 1

1

When using csv transform method asynchronously, you must invoke the callback.
If you invoke it with null, the record will simply be skipped.

if (row[1].search(/imputed/) != -1) {

  process.nextTick(function() {
    callback(
      null, 
      row[1].substr(0, 15) + "… " + row[1].search(/imputed/) + "\n"
    )
  });

} else {
  callback(null, null);
}
Sign up to request clarification or add additional context in comments.

Comments

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.