Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Calling an exported function from within the same module

Calling an exported function from within the same module

Problem

If you have a function like this in a module:

dbHandler.js

exports.connectSQL = function(sql, connStr, callback){

    ////store a connection to MS SQL Server-----------------------------------------------------------------------------------
    sql.open(connStr, function(err, sqlconn){
        if(err){
            console.error("Could not connect to sql: ", err);
            callback(false);         //sendback connection failure
        }
        else{
            callback(sqlconn);       //sendback connection object
        }
    });
}

Can you call this from inside the same module it's being defined? I want to do something like this:

later on inside dbHandler.js

connectSQL(sql, connStr, callback){
   //do stuff
});
Problem courtesy of: gjw80

Solution

Declare the function like a regular old function:

function connectSQL(sql, connStr, callback){

    ////store a connection to MS SQL Server------------------------------------
    sql.open(connStr, function(err, sqlconn){

    // ...

and then:

exports.connectSQL = connectSQL;

Then the function will be available by the name "connectSQL".

Solution courtesy of: Pointy

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Calling an exported function from within the same module

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×