misoproject / dataset

JavaScript library that makes managing the data behind client-side visualisations easy
http://misoproject.com
GNU General Public License v2.0
1.18k stars 99 forks source link

Error using Miso.Dataset with URL on node.js #208

Open protobi opened 11 years ago

protobi commented 11 years ago

Miso.Dataset works great within the browser, using on Node.js for first time and getting an error. Presumably a newbie error, but looks like the one reported in https://github.com/misoproject/dataset/issues/193.

Specifically it appears global is undefined. Was able to create the issue in a new project:

var Miso = require("miso.dataset");

// this works fine
//var ds = new Miso.Dataset({
//  data:  [
//  { color : "red",   r : 255, g : 0,   b : 0   },
//  { color : "blue",  r : 0,   g : 0,   b : 255 },
//  { color : "green", r : 0,   g : 255, b : 0   }
//]
//});

// this fails on fetch
var ds = new Miso.Dataset({
      url: 'data/MST.csv',
      delimiter: ','
    }
);

ds.fetch({
  success : function() {
    console.log("Available Columns:" + this.columnNames());
    console.log("There are " + this.length + " rows");
  }
});

Stack trace below:

/Users/pieter/Exp/influence/miso_test/node_modules/miso.dataset/dist/node/miso.ds.deps.0.4.1.js:2258
XObject ? new global.ActiveXObject("Microsoft.XMLHTTP") : new global.XMLHttpRe
                                                                    ^
TypeError: undefined is not a function
    at Object._xhrSetup.xhr (/Users/pieter/Exp/influence/miso_test/node_modules/miso.dataset/dist/node/miso.ds.deps.0.4.1.js:2258:95)
    at Function.Dataset.Xhr (/Users/pieter/Exp/influence/miso_test/node_modules/miso.dataset/dist/node/miso.ds.deps.0.4.1.js:2286:32)
    at _.extend.fetch (/Users/pieter/Exp/influence/miso_test/node_modules/miso.dataset/dist/node/miso.ds.deps.0.4.1.js:2242:15)
    at _.extend.fetch (/Users/pieter/Exp/influence/miso_test/node_modules/miso.dataset/dist/node/miso.ds.deps.0.4.1.js:1029:21)
    at Object.<anonymous> (/Users/pieter/Exp/influence/miso_test/app.js:45:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
protobi commented 11 years ago

Reading files and parsing CSV files is a specialty in itself, so delegated that part to node-csv, and all works well.

var Miso = require("miso.dataset");
var csv = require('csv');

csv()
    .from.options({columns: true})
    .from.path(__dirname+'/../public/data/MST.csv', { delimiter: ',', escape: '"' })
    .to.array( function(data){
      var ds = new Miso.Dataset({
        data: data
      });
      ds.fetch({
        success : function() {
          console.log("Dataset Ready. Columns: " + this.columnNames());
          console.log("There are " + this.length + " rows");
        }
      });
    });