Nick Stricker
Nick Stricker

Reputation: 55

Add parameter to module.exports?

I have a module that runs a query and displays it using express.

module.exports.runQuery =function(req,res){
   //gets connection
   connection.on('connect', function(err) {
      console.log("success");
      //if connection works run the request
      executeStatement();
    });
    function executeStatement() {
    request = new Request("INSERT SQL HERE", function(err, rowCount){ 

What I'd like to be able to do is pass a string containing sql in the module.exports.runquery parameters. Is there a way to do this, or an easier way to get a string of sql to executeStatement?

Upvotes: 5

Views: 21475

Answers (3)

ram
ram

Reputation: 503

Add the SQL string as another parameter.

module.exports.runQuery =function(req, res, sqlQuery){
   //gets connection
   connection.on('connect', function(err) {
      console.log("success");
      //if connection works run the request
      executeStatement(sqlQuery);
    });
    function executeStatement(sqlQuery) {
    request = new Request(sqlQuery, function(err, rowCount){

Then while calling the runQuery.

var something = require('file');
sqlQuery = "YOUR QUERY HERE";
something.runQuery(req, res, sqlQuery);

If you are using the runQuery method only as name implies. Then you won't need req, res there. Better way to do this would be

module.exports.runQuery =function(sqlQuery, callback){
   //gets connection
   connection.on('connect', function(err) {
      console.log("success");
      //if connection works run the request
      result = executeStatement(sqlQuery);
      callback(result);
    });
    function executeStatement() {
    request = new Request(sqlQuery, function(err, rowCount){
    //after finishing
    return result;
    }

Then calling would be

var something = require('file');
sqlQuery = "YOUR QUERY HERE";
something.runQuery(sqlQuery, function(result) {
    res.send(result);
});

Upvotes: 0

Arpit Solanki
Arpit Solanki

Reputation: 9931

Use exports.function_name instead of module.exports there you will be able to use whatever params you want. module.expots meant for exporting objects and not functions. Although exports.func_name can be used to export function from a file.

Example:

exports.function_name = function (params){
 . . .
}

Then use:

var helper = require('path to module')
helper.function_name(params)

Upvotes: 1

Miguel
Miguel

Reputation: 20633

You can attach a property containing the sql query in the request object:

server.js

const app = require('express')()
const {runQuery} = require('./query')

app.get('/', function (req, res) {
  req.sqlQuery = "INSERT SQL HERE"
  runQuery(req, res)
})

query.js

module.exports.runQuery = function(req, res) {
  connection.on('connect', function(err) {
    executeStatement()
  })

  function executeStatement() {
    request = new Request(req.sqlQuery, function() {})
  }
}

Or you can also do it through middlware:

function middleware(req, res, next) {
  req.sqlQuery = "INSERT SQL HERE"
  next()
}

app.get('/', middleware, runQuery)

Upvotes: 5

Related Questions