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
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
Share Improve this question asked Aug 24, 2017 at 9:53 Blaze TamaBlaze Tama 11k13 gold badges74 silver badges133 bronze badges 4-
1
Why don't you just pass it in the function's arguments, like
object.test(req, res, tableName)
? – Jeremy Thille Commented Aug 24, 2017 at 9:56 -
@JeremyThille: He/she probably wants to use the result with Express-style middleware, which will call the function with
req, res
. – T.J. Crowder Commented Aug 24, 2017 at 9:58 -
They can attach the tablename to
req
then.req.tableName="test"
– Jeremy Thille Commented Aug 24, 2017 at 10:00 -
@JeremyThille: Those e from Express, not the OP's code. It would be possible to insert another middleware function that added properties to
req
, but I wouldn't consider it good design, not least because of potential conflict. Building a function with baked-in info like this is entirely standard practice. – T.J. Crowder Commented Aug 24, 2017 at 10:03
3 Answers
Reset to default 3This 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
es 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:
- How do JavaScript closures work? - question with answers here on SO
- Closures are not plicated - a post on my anemic little blog
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
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 ining 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}`