I have a mysql.js file that accesses the database as follows:
'use strict';
var mysql = require('mysql'),
config = require('config');
var connection = mysql.createConnection({
host: config.get('mysql.server'),
user: config.get('mysql.username'),
password: config.get('mysql.password'),
database: config.get('mysql.database'),
port: config.get('mysql.port')
});
connection.connect(function (err) {
if (err) {
console.error('Error connecting: ' + err.stack);
return;
}
console.log('Connection established');
});
module.exports = connection;
I'm using the config module (config.get (mysql.server), for example) to fetch the database access data according to the template below (default.json) located at the root of the application within a folder called config:
{
"mysql": {
"database": "BANCODEDADOS",
"password": "SENHA",
"port": "PORTA",
"server": "SERVIDOR",
"username": "USUARIO"
}
}
I'm using Visual Studio to work on this project.
What's happening ...
When I press F5 inside the vscode, it usually works by accessing the database and the other resources. No problem!
When I start a restart within vscode (Ctrl + Shit + F5), it gives an error in the connection saying that it was not possible to find the information in the default.json file (where I have the access credentials as above). (config.get (mysql.server) is undefined, for example) and there, the config module returns me an undefined error:
node --debug-brk=21502 --nolazy www
Debugger listening on [::]:21502
WARNING: No configurations found in configuration directory:c:\Users\claudio.campos\Documents\Arquivos\Outros\Estudos\Projetos\Site\Aplicacao\bin\config
WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.
c:\Users\claudio.campos\Documents\Arquivos\Outros\Estudos\Projetos\Site\Aplicacao\node_modules\config\lib\config.js:181
throw new Error('Configuration property "' + property + '" is not defined');
^
Error: Configuration property "mysql.server" is not defined
at Config.get (c:\Users\claudio.campos\Documents\Arquivos\Outros\Estudos\Projetos\Site\Aplicacao\node_modules\config\lib\config.js:181:11)
at Object.<anonymous> (c:\Users\claudio.campos\Documents\Arquivos\Outros\Estudos\Projetos\Site\Aplicacao\db\mysql.js:7:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (c:\Users\claudio.campos\Documents\Arquivos\Outros\Estudos\Projetos\Site\Aplicacao\routes\categoria.js:5:13)
Can anyone tell me why this only happens when I give a restart in vscode?