我的要求没有结果
No results on my request
我正在开发一个小型 Nodejs/Express 网站,该网站通过 Tedious 请求 SQL 服务器数据库。
当我在任何函数之外创建我的连接时,它似乎有效但我无法刷新。
当我尝试将它放入我的 connx 函数时,没有附加任何内容,它似乎没有按 conn.on('connect'...)
顺序输入。
我很迷茫。希望你能帮助我。
var http = require('http');
var jade = require('jade');
var moment = require('moment');
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var express = require('express');
var app = express();
var config = {
server: 'myserver',
userName: 'myuser',
password: 'mypassword',
options: {
instanceName: 'myinstance',
database: 'mydb',
}
};
app.set('views','./views');
app.set('view engine','jade');
app.use('/public',express.static(__dirname + '/public'));
var results = [];
function requete1(dated, datef) {
return ligne = "select Batch, Parametres, Etat, Durée, Resultat " +
"from LancementsNuit where HeureDebut >= '"+dated+" 18:00:00' " +
"and HeureFin <= '"+datef+" 18:00:00'";
}
function connx(x_date1,x_date2) {
var conn = new Connection(config);
//flush de result
result = [];
conn.on('connect', function () {
console.log(requete1(x_date1,x_date2));
var request = new Request(requete1(x_date1,x_date2), function(err, rowCount) {
if (err)
console.error(err);
});
request.on('row', function(row) {
results.push({
batch: row[0].value,
parametres: row[1].value,
etat: row[2].value,
duree: row[3].value,
resultat: row[4].value
})
});
conn.execSql(request);
conn.close();
})
}
app.get('/', function (req, res) {
var datef = moment().format('YYYY-MM-DD');
console.log(datef);
var Weekday = moment().isoWeekday();
if (Weekday == "1") {
var dated = moment(datef,'YYYY-MM-DD').subtract(3, 'd').format('YYYY-MM-DD');
} else {
var dated = moment(datef,'YYYY-MM-DD').subtract(1, 'd').format('YYYY-MM-DD');
}
connx(dated, datef);
console.log(results);
res.render('index', { data: results,
dated: dated,
datef: datef
});
})
app.get('/:dated', function (req, res) {
// console.log(results);
var datef = req.params.dated;
var Weekday = moment(datef,'YYYY-MM-DD').isoWeekday();
if (Weekday == "1") {
var dated = moment(datef,'YYYY-MM-DD').subtract(3, 'd').format('YYYY-MM-DD');
} else {
var dated = moment(datef,'YYYY-MM-DD').subtract(1, 'd').format('YYYY-MM-DD');
}
console.log("dated = " + dated + "; datef = " + datef);
connx(dated, datef);
console.log(results);
res.render('index', { data: results,
dated: dated,
datef: datef
});
})
app.listen(Number(process.argv[2]));
conn.on('connect')
是异步的,但它在同步函数内 - connx
。这意味着 res.render
在它完成之前被调用,所以你最终得到 result = []
。您需要将回调传递给 connx
并确保它在 conn.on('connect')
完成时被调用。
看看 this link,它会为您指明正确的方向。
我试过了
connx(dated, datef, function() {
res.render('index', {
data: results,
dated: dated,
datef: datef
});
});
和
function connx(dated,datef,callback) {
var conn = new Connection(config);
conn.on('connect', function () {
results = [];
var request = new Request(requete1(dated,datef), function(err, rowCount) {
if (err)
console.error(err);
else
callback();
});
request.on('row', function(row) {
results.push({
batch: row[0].value,
parametres: row[1].value,
etat: row[2].value,
duree: row[3].value,
resultat: row[4].value
})
});
conn.execSql(request);
request.on('done', function() {
console.log('ici');
conn.close();
});
});
}
看起来不错。谢谢
我正在开发一个小型 Nodejs/Express 网站,该网站通过 Tedious 请求 SQL 服务器数据库。
当我在任何函数之外创建我的连接时,它似乎有效但我无法刷新。
当我尝试将它放入我的 connx 函数时,没有附加任何内容,它似乎没有按 conn.on('connect'...)
顺序输入。
我很迷茫。希望你能帮助我。
var http = require('http');
var jade = require('jade');
var moment = require('moment');
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var express = require('express');
var app = express();
var config = {
server: 'myserver',
userName: 'myuser',
password: 'mypassword',
options: {
instanceName: 'myinstance',
database: 'mydb',
}
};
app.set('views','./views');
app.set('view engine','jade');
app.use('/public',express.static(__dirname + '/public'));
var results = [];
function requete1(dated, datef) {
return ligne = "select Batch, Parametres, Etat, Durée, Resultat " +
"from LancementsNuit where HeureDebut >= '"+dated+" 18:00:00' " +
"and HeureFin <= '"+datef+" 18:00:00'";
}
function connx(x_date1,x_date2) {
var conn = new Connection(config);
//flush de result
result = [];
conn.on('connect', function () {
console.log(requete1(x_date1,x_date2));
var request = new Request(requete1(x_date1,x_date2), function(err, rowCount) {
if (err)
console.error(err);
});
request.on('row', function(row) {
results.push({
batch: row[0].value,
parametres: row[1].value,
etat: row[2].value,
duree: row[3].value,
resultat: row[4].value
})
});
conn.execSql(request);
conn.close();
})
}
app.get('/', function (req, res) {
var datef = moment().format('YYYY-MM-DD');
console.log(datef);
var Weekday = moment().isoWeekday();
if (Weekday == "1") {
var dated = moment(datef,'YYYY-MM-DD').subtract(3, 'd').format('YYYY-MM-DD');
} else {
var dated = moment(datef,'YYYY-MM-DD').subtract(1, 'd').format('YYYY-MM-DD');
}
connx(dated, datef);
console.log(results);
res.render('index', { data: results,
dated: dated,
datef: datef
});
})
app.get('/:dated', function (req, res) {
// console.log(results);
var datef = req.params.dated;
var Weekday = moment(datef,'YYYY-MM-DD').isoWeekday();
if (Weekday == "1") {
var dated = moment(datef,'YYYY-MM-DD').subtract(3, 'd').format('YYYY-MM-DD');
} else {
var dated = moment(datef,'YYYY-MM-DD').subtract(1, 'd').format('YYYY-MM-DD');
}
console.log("dated = " + dated + "; datef = " + datef);
connx(dated, datef);
console.log(results);
res.render('index', { data: results,
dated: dated,
datef: datef
});
})
app.listen(Number(process.argv[2]));
conn.on('connect')
是异步的,但它在同步函数内 - connx
。这意味着 res.render
在它完成之前被调用,所以你最终得到 result = []
。您需要将回调传递给 connx
并确保它在 conn.on('connect')
完成时被调用。
看看 this link,它会为您指明正确的方向。
我试过了
connx(dated, datef, function() {
res.render('index', {
data: results,
dated: dated,
datef: datef
});
});
和
function connx(dated,datef,callback) {
var conn = new Connection(config);
conn.on('connect', function () {
results = [];
var request = new Request(requete1(dated,datef), function(err, rowCount) {
if (err)
console.error(err);
else
callback();
});
request.on('row', function(row) {
results.push({
batch: row[0].value,
parametres: row[1].value,
etat: row[2].value,
duree: row[3].value,
resultat: row[4].value
})
});
conn.execSql(request);
request.on('done', function() {
console.log('ici');
conn.close();
});
});
}
看起来不错。谢谢