M
M
ML2016-05-12 20:13:02
Node.js
ML, 2016-05-12 20:13:02

nodejs scope?

Created mysql.js file,
put mysql module there:
var mysql = require( 'mysql' );
After I created the config.js file I
connected it after I connected the module.
In the config.js file I
registered connect to the database and got an error mysql undefined,
how so?
In order for everything to work, I connected mysql already in the config.js
Connect file, I declared the db variable.
And then I called it all in the server.js file, and again db is not defined.
That is: server.js file

var mysql = require( 'mysql' );
require( './config' );
db.query( 'SELECT * FROM name', function( error, result, fields ) {
} );

//error db - undefined

But db is defined in config.js

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Super User, 2016-05-12
@staffID

All files connected via require have their own scope.

M
Maxim, 2016-05-12
@maxfarseer

So your db variable is not defined here.
That is, if in config.js , for example {param1: 'qweqwe', param2: 'ababaa'}, then in server.js it should be
either
var param1 = require('./config').param1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question