我正在尝试从sql server数据库中获取数据(我正在使用mssql模块执行此操作:https://www.npmjs.com/package/mssql)并将其发送到Redis。 所以我创建了三个文件:
我想流式传输sql_server数据库,并将结果发送给server.js,以便返回每一行。 然后server.js文件将结果传递给redis函数,该函数将创建一对键/值。
但我对node.js不太熟悉,我不知道如何将查询结果返回给每一行的server.js:
这是 sql_server.js 函数来流式传输数据库
execRequests: function(requestEntry, flux){
try{
const request = new sql.Request(pool)
request.stream = true // You can set streaming differently for each request
request.query(requestEntry)// or request.execute(procedure)
request.on('recordset', columns => {
//console.log(columns);
})
request.on('row', row => {
// THAT I AM DOING FOR NOW
//redisMethods.setValue(JSON.stringify(row), flux);
// THAT I WOULD LIKE TO TO
//return row;
})
request.on('error', err => {
// May be emitted multiple times
})
request.on('done', result => {
console.log("done for flux :" + flux);
})
} catch (err){
console.log(err);
}
}
这是 server.js
sqlMethods.connection().then(function(pool){
try{
if(pool._connected){
console.log("Connected to SQL Server database : " + pool.config.database);
// connection to redis database
redisMethods.connection();
// get the requests to execute
sqlMethods.getRequests().then(function(requestArray){
// loop over the array containing requests
for (var i in requestArray){
let request = requestArray[i].requete; //extract the request
let flux = requestArray[i].flux; //extract the flux name
sqlMethods.execRequests(request, flux).then(function(result){
// redisMethods.setValue(JSON.stringify(result), flux);
});
}
});
}
} catch (err){
console.log(err);
}
希望我的解释是可以理解的,我需要你的帮助才能找到一种方法将行从sql_server.js返回到server.js。 谢谢!
答案 0 :(得分:1)
我可以建议您使用回调函数来传递错误或数据。
execRequests: function(requestEntry, flux, callback){
const request = new sql.Request(pool);
request.stream = true; // You can set streaming differently for each request
request.query(requestEntry); // or request.execute(procedure)
var errors = [];
var rows = [];
request.on('recordset', columns => {
//console.log(columns);
});
request.on('row', row => {
// THAT I AM DOING FOR NOW
//redisMethods.setValue(JSON.stringify(row), flux);
// THAT I WOULD LIKE TO TO
//return row;
rows.push(row);
});
request.on('error', err => {
// May be emitted multiple times
errors.push(err);
});
request.on('done', result => {
console.log("done for flux :" + flux);
callback(errors, rows);
});
}
然后在你的server.js
sqlMethods.connection().then(function(pool) {
try {
if (pool._connected) {
console.log("Connected to SQL Server database : " + pool.config.database);
// connection to redis database
redisMethods.connection();
// get the requests to execute
sqlMethods.getRequests().then(function (requestArray) {
// loop over the array containing requests
for (var i in requestArray) {
let request = requestArray[i].requete; //extract the request
let flux = requestArray[i].flux; //extract the flux name
sqlMethods.execRequests(request, flux, function (errors, rows) {
if (errors && errors.length) {
// There is errors
}
else if (rows && rows.length) {
// There is no errors but rows
}
});
}
});
}
} catch (err) {
console.log(err);
}
});
希望它有所帮助。