I have a code snippet in db.js as below,
exports.asyncGetAllData = function (cb) {
connection.connect(function(err) {
connection.query("SELECT * FROM prices WHERE=" + "'" + ID + "'", function (err, result) {
//console.log("info:" + cb);
if (err) console.log("Error: " + err);
else
{
cb(result);
}
});
});
};
I want to pass and ID from app.js into asyncGetAllData function in db.js . The following code snippet is in app.js
app.get('/test/getPriceTrend/:parameter', function(req, res) {
console.log('SERVER::testGetPriceTrend');
console.log(req.url);
var str=req.url;
db_connection.asyncGetAllData(function(data) {
var obj = str.split("&");
var ID = obj[0].split("/")[3];
console.log(JSON.stringify(data));
res.setHeader('Accept', 'application/json');
res.writeHead(res.statusCode);
//The following piece of code will send information from the database
res.write("hello");
res.end();
});
});
In the above-mentioned code (app.js), I have parsed ID from a get request. I want to send this ID to asyncGetAllData function which resides in db.js. How can I send ID parameter and fetch the result?
Thanks in advance,
You can just extend the function with an additional argument
exports.asyncGetAllData = function (cb, ID) {
connection.connect(function(err) {
connection.query("SELECT * FROM prices WHERE=" + "'" + ID + "'" ...
And then pass it when you call the function in app.js
var str = req.url;
var obj = str.split("&");
var ID = obj[0].split("/")[3];
db_connection.asyncGetAllData(function(data) {
...
}, ID);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.