Node.js + node-tds + Sql Server

I'm trying to follow the node-tds example on how to connect and retrieve data from Sql server with node.js http://cretz.github.com/node-tds/

I got no error on connect, but when I try to retrieve data I get the following:

"Error: Client must be in LOGGED IN state before executing sql
at TdsClient.sqlBatch (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds-client.js:101:13)
at Statement.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds.js:256:37)
at Statement.execute (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds.js:2:59)
at Object.<anonymous> (C:\Program Files (x86)\nodejs\edas\app.js:67:6)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)"

I'm logged in, so… I can't figure out what is happening.

You should execute your statement when the connection has already established, i.e.

var tds = require("tds");

var conn = new tds.Connection({
  host: "localhost",
  port: 1433,
  userName: "user",
  password: "secret",
  database: "MyDb"
});

conn.connect(function(error) {
  if (error != null) {
    console.error("Received error", error);
  } else {
    console.log("Now connected, can start using");
  }
});

conn.on("connect", function() {
    var statement = conn.createStatement("SELECT ProductID, ProductName FROM Products");
    statement.on("row", function(row) {
      console.log("Received row: ", row.getValue(0));
    });
    statement.execute();
});