2017-07-16 37 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); 
相关问题