Blaze Tama
Blaze Tama

Reputation: 10948

How to pass parameter/arguments to functions in node js

First, I'm just getting started with node js (look at the question), so please bear with me

This is a case I made to make my question clearer. I made a function to be called on another JS :

exports.test = function(req, res){
  connection.query('SELECT * FROM `test`', function (error, results) {
  console.log(results);
  });
};

Then I can call it with object.test();

I want to generalize this function, by passing the table name from another JS, instead of hardcoding it. How to do that?

In Java, I could googling about this easily. However, on Node, almost all search results telling about the parameter in the url (POST/GET), but my need is to just passing a param/args to a function.

Thanks in advance

Upvotes: 0

Views: 18624

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1073968

This isn't really a Node question, it's a JavaScript question.

You can create a function that returns a function. You pass the table name to the builder, and then use it in the function that builder creates:

exports.makeTest = function(tableName) {
  return function test(req, res){
    connection.query('SELECT * FROM `' + tableName + '`', function (error, results) {
      console.log(results);
    });
  };
};

Note: I assume tableName comes from code you control and can trust; otherwise, string concatenation is not acceptable in the above.

You'd use it like this:

var test = makeTest("test");

...and then call test with req and res (or more likely, pass it to something like Express that will).

Here's a non-Node example just to show how the parts work:

function makeTest(tableName) {
  return function test(req, res) {
    console.log("Table name is:", tableName);
    console.log("req is:", req);
    console.log("res is:", res);
  };
}

var test = makeTest("test");
console.log("Call 1:");
test({name: "req1"}, {name: "res1"});
console.log("Call 2:");
test({name: "req2"}, {name: "res2"});
.as-console-wrapper {
  max-height: 100% !important;
}

It may seem surprising that the tableName argument is still accessible to the test function after makeTest returns. That's the nature of JavaScript's closures. You can read more about closures here:

Upvotes: 3

Anshul Sahni
Anshul Sahni

Reputation: 666

you can use the concept of Higher order function in this

module.exports = function(tableName) {
  return function(req, res) {
    //...here you have tableName accessible
  };
};

And in the routes (if you're following the general flow of express app) where you are applying this controller,

const somethingController = require('/path/to/file');
route.get('/something', somethinController('test')); //...pass the table Name

Upvotes: 1

Alex Naish
Alex Naish

Reputation: 120

So there are multiple points here.

Firstly, your test function takes two parameters - a req and a res. Assuming you're using express to create a HTTP server this symbolises the incoming request and the outbound response. So I would read up on how express attaches things like POST data or query parameters in order to allow you to pass dynamic data into a route handler.

Secondly, you can interpolate strings in Javascript with template literals. For instance:

`select * from ${myTableParameter}`

Upvotes: 0

Related Questions