0
var iniparser = require('iniparser');
var config = iniparser.parseSync('../Config/init/db.ini');
var env = process.env.NODE_ENV || 'dev'; //startup nodejs with e.g:  NODE_ENV= node server.js

var app                 = require('http').createServer(handler),
io                  = require('socket.io').listen(app),
fs                  = require('fs'),
mysql               = require('mysql'),
connectionsArray    = [],
connection          = mysql.createConnection({
    host        : config[env]['host'],
    user        : config[env]['user'],
    password    : config[env]['pwd'],
    database    : config[env]['dbname']
}),
POLLING_INTERVAL = 3000,
pollingTimer;    

console.log(config[env]['host']);
console.log(config[env]['user']);
console.log(config[env]['pwd']);
console.log(config[env]['dbname']);

// If there is an error connecting to the database
connection.connect(function(err) {
  // connected! (unless `err` is set)
  console.log( err );
});

db.ini

 [dev]
 host = "localhost"
 user = "root"
 pwd =  "root"
 dbname = "db_local"

output from console.log:

"localhost"
"root"
"root"
"tittilate_local"

This top part of my nodejs script. console.log is outputting fine but connection is not established. Somehow the config variable is not recognized within createConnection.

Anybody has a suggestion ?

Bart
  • 91
  • 3
  • 11
  • Do you actually run `connection.connect()` somewhere? I assume that the `config` variable contains all the proper values. – robertklep Apr 22 '13 at 13:32
  • Yes sure, if I replace the config parameters with strings all is fine. – Bart Apr 22 '13 at 13:33
  • I am able to connect fine if I use: host: "localhost" etc.. – Bart Apr 22 '13 at 13:43
  • Could you post your INI file as well? (I removed my previous comment because it didn't make sense, you already said you could connect fine using strings :) – robertklep Apr 22 '13 at 13:43

1 Answers1

2

INI file values shouldn't be delimited by quotes, otherwise those quotes will become part of the value.

So in your case, try this:

[dev]
host   = localhost
user   = root
pwd    = root
dbname = db_local
robertklep
  • 198,204
  • 35
  • 394
  • 381