[英]How to send an ID to the database for searching in callback function
我在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到asyncGetAllData功能db.js。 以下代碼段位於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();
});
});
在上面提到的代碼(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 + "'" ...
然后在調用app.js中的函數時將其傳遞
var str = req.url;
var obj = str.split("&");
var ID = obj[0].split("/")[3];
db_connection.asyncGetAllData(function(data) {
...
}, ID);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.