1

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

5
  • 1
    You export correctly. con is undefined in your db_function.js Commented Jan 17, 2018 at 12:09
  • i declared var con = config.conn; Commented Jan 17, 2018 at 12:11
  • And it didn't occur to you that con is probably undefined? Commented Jan 17, 2018 at 12:13
  • @MaheshJagdale I trust what Javascript says. Can you show us your entire db_function? Maybeyou declare con too late, and JS already tries to access it Commented Jan 17, 2018 at 12:14
  • con is declared in app.js, yet you try to access it in db_function.js. Even if you call your function in app.js, javascript still (thankfully) maintains the correct context for code evaluation and execution Commented Jan 17, 2018 at 12:20

3 Answers 3

5

You can export like this:

enter code here
var funName = function() {
}
module.exports = funName;
Sign up to request clarification or add additional context in comments.

1 Comment

Weird how this answer got 2 upvotes and is not even answering the question
2

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);
  });
});

2 Comments

any other way..?
yes, give is_valid_IP function your connection, updating answer
0

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;

1 Comment

what if other way around: update module.exports, but not export ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.