Reputation: 174
i am new in nodejs,i need to export function in nodejs
db_function.js file
var mysql = require('mysql');
var config = require('./config.js');
var con = config.conn;
exports.is_valid_IP = {
function(IP,callback)
{
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM server_master", function (err, result, fields) {
if (err) throw err;
callback(result);
});
});
}
};
app.js file
app.get('/test',function(req,res){
var IP = 1;
db.is_valid_IP(IP,function(result){
console.log(result);
});
});
It Show error Cannot call method 'connect' of undefined
Upvotes: 1
Views: 3877
Reputation: 30079
Let's simplify the issue, as the title of the question suggests, "how to export function in nodejs". If we only consider how to export function in Node.js, we can do as below:
./db_function.js (We export function isValidIp):
'use strict';
module.exports.isValidIp = function(ip, callback) {
console.log("Check if IP is valid.");
callback();
};
./app.js
'use strict';
var db = require('./db_function');
// require() returns the object that module.exports references
db.isValidIp('127.0.0.1', function(){
console.log('Called back.');
});
Run it with command node app.js
, you'll get:
Check if IP is valid.
Called back.
One caveat
./db_function.js (This version does not work):
'use strict';
exports = {
isValidIp: function(ip, callback) {
console.log("Check if IP is valid.");
callback();
}
};
You'll get TypeError:
TypeError: db.isValidIp is not a function
That is because require() return module.exports, rather than exports. exports is just a shorthand of module.exports. If you point exports to a different object, you should update module.exports as well. The following version will work:
'use strict';
exports = {
isValidIp: function(ip, callback) {
console.log("Check if IP is valid.");
callback();
}
};
module.exports = exports;
Upvotes: 0
Reputation: 8199
Let me sum up the comments:
You export correctly. That's not where your problem lies
The issue occurs due to the fact that, in db_function.js
, con
is not defined.
Even if you declare it in app.js
, javascript will correctly isolate the two contexts (app.js
from db_function.js
).
We talk about different files here, but the context preservation can occur even in the same files
var functionOne = function(){
var con = 1;
}
console.log(con) // undefined err, because con is declared inside the function
even if you call the function
var functionOne = function(){
var con = 1;
}
functionOne();
console.log(con) // undefined err, because con is long gone as soon as function returns
con
will live only until the function returns.
if you want to tell is_valid_IP
what connection to use, you can simply update your code as follows:
db_function.js file
exports.is_valid_IP = {
function(IP,con,callback)
{
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM server_master", function (err, result, fields) {
if (err) throw err;
callback(result);
});
});
}
};
And then use it in app.js file as follows:
app.get('/test',function(req,res){
var IP = 1;
db.is_valid_IP(IP,con,function(result){
console.log(result);
});
});
Upvotes: 2
Reputation: 167
You can export like this:
enter code here
var funName = function() {
}
module.exports = funName;
Upvotes: 5