I'm trying to use connection
from a connection.js file and use it in different file webFrontend.js using exports
object. Now what I get on running server is:
{
"Result": "undefinedThis is result"
}
That means connection
is not defined. Why is it happening? connection
is working fine if getConnection
is created in same (webFrontend.js) file, but the problem is when I use getConnection
in same exports function in connection.js hence the connection
not defined error:
Here are 2 necessary files (routes file has no problem) that explains what I'm doing:
connection.js
var mysql = require('mysql');
exports.connExport = function () {
var connectionPool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
database: 'rockcity_followme'
});
if(connectionPool) {
connectionPool.getConnection(function (err, connection) {
if (err) {
return err;
} else {
return connection;
}
});
}else{
var abc="return error";
return abc;
}
}
webFrontend.js
var connObj=require('../Routes/connection.js');
var connection=connObj.connExport();
exports.getIndivRecords= function(req, res, next){
res.send({
Result: connection+"This is result"
});
return next();
};