我不知道这个问题。我试图找到一些东西,但还是不知道。 这是我的代码
function part1(sql, controltime, headers_view, results_view, tmp){
var timerName = "QueryTime";
var request = new Request(sql, function(err) {
if (err) {
console.log(err);
}
});
request.on('doneProc', function(rowCount, more, rows) {
if(!more){
console.timeEnd(timerName);
}
});
var result = "";
var header = "";
request.on('row', function(columns) {
headers_view = '\n'
results_view += '\n'
columns.forEach(function(column) {
if (column.value === null) {
result = '0';
} else {
header = column.metadata.colName;
result = column.value;
}
headers_view += header + ',';
results_view += result + ',';
});
headers_view = headers_view.replace(',\n','\n').slice(0,-1);
results_view = results_view.replace(',\n','\n').slice(0,-1);
tmp = controltime;
});
console.log(request)
return {headers_view:headers_view, results_view:results_view, tmp:tmp, request:request};
}
console.log(请求)结果以上
Request {
_events: [Object: null prototype] { doneProc: [Function], row: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
sqlTextOrProcedure: " SELECT [Head],round([Avg],2) as 'Class_Top' FROM [Class1].[dbo].[Class1_Control] WHERE DateTime='2020-07-26 12:30:32' ",
parameters: [],
parametersByName: {},
originalParameters: [],
preparing: false,
canceled: false,
paused: false,
userCallback: [Function],
handle: undefined,
error: undefined,
connection: undefined,
timeout: undefined,
rows: undefined,
rst: undefined,
rowCount: undefined,
callback: [Function],
[Symbol(kCapture)]: false
}
这个结果就是我想要的。但是问题是
async function exec1(sql, controltime) {
part1(sql, controltime, headers_view1, results_view1, tmp1);
var headers_view1 = part1.headers_view;
var results_view1 = part1.results_view;
var tmp1 = part1.tmp;
var request = part1.request;
console.log('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&');
console.log(part1.request);
console.log('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&');
console.log('====================================')
console.log(request)
console.log('====================================')
//console.time(timerName);
const query = await Promise.all([
dbconn1.execSql(request)
]);
}
console.log(request)和(part1.request)以上均未定义 因此,我尝试更改异步->同步
function exec1(sql, controltime) {
part1(sql, controltime, headers_view1, results_view1, tmp1);
var headers_view1 = part1.headers_view;
var results_view1 = part1.results_view;
var tmp1 = part1.tmp;
var request = part1.request;
console.log('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&');
console.log(part1.request);
console.log('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&');
console.log('====================================')
console.log(request)
console.log('====================================')
//console.time(timerName);
dbconn1.execSql(request)
}
但仍未定义... 有人告诉我尝试使用成功/失败方法 问题是
感谢您阅读这个冗长的问题
答案 0 :(得分:0)
part1是一个函数,但是您试图记录返回值。
在第一行中,必须将part1的返回值存储在某些内容中,然后再记录something.request。
所以第一行应该是这样的
var something = part1(sql,controltime,headers_view1,results_view1,tmp1);
然后
console.log(something.request)