2017-07-16 36 views
0

我在db.js下面的代碼片段搜索,如何將ID發送到數據庫中的回調函數

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)中,我從獲取請求中解析了ID。我想把這個ID發送到駐留在db.js中的asyncGetAllData函數。我如何發送ID參數並獲取結果?

由於提前,

回答

1

你可以只是一個額外的參數擴展功能

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); 
相關問題