Tedious js不在Express路由内进行连接

时间:2018-10-24 00:44:57

标签: javascript node.js sql-server express tedious

我正在使用Express和Tedious.js对我的Azure数据库进行查询。在Express路由之外调用函数“ getPosicoes()”可以正常运行,而在Express路由内部调用时不会运行。似乎挂在“ connection.on(“ connect”)“上。

function getPosicoes(){
    console.log("entered function");
    return new Promise(function(resolve, reject){
        connection.on('connect', function(err){
            if (err) {
                log.error('connection error:', err);
                reject(err);
            } else {
                console.log("success")
                request = new Request('SELECT * FROM VIEWPOSICOES ', function(err, rowCount, rows) {  //EXEC dbo.crud_PaisesInsert @Pais = @loginvar, @PaisID = @passvar
                if (err) {  
                    console.log("ERROR REQ: "+err);
                    reject(err);
                }  
                else {
                    console.log('Request created successfully');
                    resolve(rows)
                }
                });  
                request.addParameter('CID', TYPES.Int, 3);

                var result = "";  
                request.on('row', function(columns) {  
                    columns.forEach(function(column) {  
                    if (column.value === null) {  
                        console.log('NULL');  
                    } else {  
                        result+= column.value + " ";  
                    }  
                    });  
                    console.log("getPosicoes: " +result);  
                    result ="";  
                }); 

                    request.on('returnValue', (parameterName, value, metadata) => {
                        log.info(parameterName + ' = ' + value);
                    });
                connection.execSql(request);                 
            }
        });
    });
}

 app.get('/getPositions', function(req, res, next){
    walletID = req.body.walletID;
    console.log("getPositions");

    getPosicoes()
    .then((paramter)=> {
      console.log('gp success');
      res.status(200).json({ message: 'gp success.' });
    })
    .catch((e)=> {
        console.log('gp Failed', e);  //<== I thought this log be written.  But not.
      res.status(400).json({ message: 'gp failed.' });
    });
 });

0 个答案:

没有答案