如何向数据库发送 ID 以便在回调函数中进行搜索



我在db中有一个代码片段.js如下所示,

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);
}
});
});
};

我想将 ID 从app.js 传递到db.js中的 asyncGetAllData 函数。以下代码片段位于应用中.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();
});

}(;

在上述代码 (app.js( 中,我从 get 请求中解析了 ID。我想将此ID发送到驻留在db.js中的asyncGetAllData函数。如何发送 ID 参数并获取结果?

提前感谢,

你可以用一个额外的参数来扩展函数

exports.asyncGetAllData = function (cb, ID) {
connection.connect(function(err) {
connection.query("SELECT * FROM prices WHERE=" + "'" + ID + "'"  ...

然后在应用程序中调用函数时传递它.js

var str = req.url;
var obj = str.split("&");
var ID  = obj[0].split("/")[3];
db_connection.asyncGetAllData(function(data) {
...
}, ID);

最新更新