0

I want to achieve that node prints me a table from MySql on my Windows 7 (64bit) machine but fail. Sorry for the long post in advance, I tried to be complete, also I failed to find this error in the forum. The optimistic mysql.js file I typed in Notepad++ was

var mysql = require('node-mysql');
var connectParams = {'hostname' : 'localhost','user' : 'dev','password': 'dev','database' : 'upandrunning'};
var db = new mysql.Database(connectParams);
db.connect( function(err)   {
    if(err) return console.log('Failed to connect ' + err);

    this.query()
    .select(['id', 'user_login'])
    .from('users')
    .execute(
        function(err, rows, columns){
            if(err) return console.log('err on query '  + err);

            console.log(rows);
        })});

Node keeps returning

C:\dev\nodejs\0.10\tutorials\node-up-and-running>node tutorials\node-up-and-running\mysql.js

module.js:340
throw err;
      ^
Error: Cannot find module 'C:\dev\nodejs\0.10\tutorials\node-up-and-running\tutorials\node-up-and-running\mysql.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3

Why??

Before the node command I told Squirrel to do

drop database if exists upandrunning;
create database upandrunning;
grant all privileges on upandrunning.* to 'dev'@'%' identified by 'dev';
use upandrunning;
create table users(
id int auto_increment primary key,
user_login varchar(25),
user_nicename varchar(75)
);

using my localhost root account (bad), but all seemed fine because squirrel object tab showed new upandrunning db. Node.exe is on C:\dev\nodejs\0.10\ but from dos prompt:

C:\dev\nodejs\0.10\tutorials\node-up-and-running>node -v

gives

v0.10.26

My file mysql.js has been reduced to the mere

var mysql = require('node-mysql');

where the error must be. I ran the following commands from C:\dev\nodejs\0.10\tutorials\node-up-and-running>

npm install -g mysql
npm install -g node-mysql
npm install -g db-mysql

These were the ones i could find. After each install the same error above.

Any suggestions please how to make node cooperate?

I admit I lack node trouble shooting skills, but studying module.js, should not be necessary. Also, do you know of, and enjoy working in, a free and easy to work with code completion ide for node? And is there some way to debug other than console.log() ?. Lastly what are your ideas on layout conventions for reducing the closing brackets because they hurt the eyes.

The site suggested From NodeJS require a global module/package but how do i get the node_path variable on dos prompt and how to set it: C:\dev\nodejs\0.10\tutorials\node-up-and-running>set NODE_PATH

Environment variable NODE_PATH not defined

Ok set NODE_PATH=c:\dev\nodejs\0.10 C:\dev\nodejs\0.10\tutorials\node-up-and-running>node tutorials\node-up-and-running\mysql.js

 'node' is not recognized as an internal or external command, operable program or batch file.

Closing and reopening dos makes this go away.

C:\dev\nodejs\0.10>set NODE_PATH=%NODE_PATH%;C:\dev\nodejs\0.10\node_modules

And it works.. But now it fails on

var mysql = require('mysql');
var connectParams = {'hostname' : 'localhost','user' : 'dev','password': 'dev','database' : 'upandrunning'};
var db = new mysql.Database(connectParams);

C:\dev\nodejs\0.10\mysql.js:3
var db = new mysql.Database(connectParams);
         ^
TypeError: undefined is not a function
at Object.<anonymous> (C:\dev\nodejs\0.10\mysql.js:3:10)
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)
at startup (node.js:119:16)
at node.js:902:3

and if i remove the new it says

C:\dev\nodejs\0.10\mysql.js:3 var db = mysql.Database(connectParams); ^ TypeError: Object # has no method 'Database' at Object. (C:\dev\nodejs\0.10\mysql.js:3:17)

Using the REPL:

var mysql = require('mysql'); undefined var connectParams = {'hostname' : 'localhost','user' : 'dev','password': 'dev' ,'database' : 'upandrunning'}; undefined var db = new mysql.Database(connectParams); TypeError: undefined is not a function at repl:1:10 at REPLServer.self.eval (repl.js:110:21) at repl.js:249:20 at REPLServer.self.eval (repl.js:122:7) at Interface. (repl.js:239:12) at Interface.EventEmitter.emit (events.js:95:17) at Interface._onLine (readline.js:202:10) at Interface._line (readline.js:531:8) at Interface._ttyWrite (readline.js:760:14) at ReadStream.onkeypress (readline.js:99:10)

Community
  • 1
  • 1

1 Answers1

0

Aren't you supposed to create the connection like this.. ?

connection = mysql.createConnection({
                   host : 'localhost',
                   user : 'node',
                   password : 'UnPassword',
                   database: 'nodesql'
             });
martskins
  • 2,920
  • 4
  • 26
  • 52