0

我正在开发一个通过 Tedious 请求 SQL Server 数据库的小型 Nodejs/Express 网站。

当我在任何功能之外创建连接时,它似乎可以工作,但我无法刷新。

当我尝试将它放在我的 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]));
4

2 回答 2

0

conn.on('connect')是异步的,但它在同步函数内部 - connx. 这意味着res.render在它完成之前被调用,所以你最终得到result = []. 您需要将回调传递给connx并确保在conn.on('connect')完成时调用它。

看看这个链接,它会为你指明正确的方向。

于 2015-08-24T14:05:08.757 回答
0

我试过这个

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();
    });
  });
}

看起来不错。谢谢。

于 2015-08-24T15:03:02.290 回答