0

This is my code:

var csv = require('csv');

var Loader = function() {
  var rows;
  csv()
  .from.path('./data/ebay.csv', {
    columns: true,
    delimiter: ';'
  })
  .to.array( function(rows) {
    setRows(rows);
  });
  function setRows(input) {
    rows = input;
  }
  return rows;
};

module.exports = Loader;

I want to get rows when I call Loader object. I'am beginner in OOP Javascript, so I have no idea what to do it. Where do I start learning javascript oop with node? I found many tutorials that describe how to start with node and how to make webs using various frameworks, but this I already know. I programmed in PHP and now moving to NodeJS and I'm wasted.

2
  • 3
    Welcome to the wonderful world of async! You need to use a callback. Commented Feb 12, 2013 at 15:47
  • and unpredictable world full of mystery. Commented Feb 12, 2013 at 16:12

1 Answer 1

1

Like all node.js functions that deal with I/O csv works asynchronously. Therefore, the call csv.from..to.. returns immediately, but the callback function is only called later. Make your Loader asynchronous as well, like this:

var csv = require('csv');
var Loader = function(onData) {
  csv()
  .from.path('./data/ebay.csv', {
    columns: true,
    delimiter: ';'
  })
  .to.array(onData);
};
module.exports = Loader;
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.