Requests can only be made in the LoggedIn state, not the Connecting state

99封情书 提交于 2019-12-25 01:53:20

问题


I am trying to connect to SQL Server (2016) using the "tedious" javascript library - I see a few of us had the same issue but not many of them had the question resolved - can you'll please let me know what I am doing wrong here?

To give a background in short, I am spinning up my ApolloServer ( GraphQL - ) to build my schemas and have apollo server talk to my SQL Server and display it on my front end using apollo client. I am able to get my ApolloServer running - but when I try to connect to SQL Server to get my data - there's my issue.

var Connection = require('tedious').Connection;  
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
    var config = {  
        server: 'SQLDEV\DEV',  //update me
        authentication: {
             type: 'default',
            options: {               
                trustedConnection: true, // my dev db server uses windows authentication
            }
        },
        options:{
            debug:{
                packet:true,
                data:true,
                payload:true,
                token:false,
                log:true,
            },
            rowCollectionOnRequestCompletion : true,
            database: 'XXX',
        }

    };  
    var connection = new Connection(config);  
    connection.on('connect', function(err) {        
        console.log("Connected"); 
        executeStatement(); 
    });  



    function executeStatement() {
        request = new Request("Select * from dbo.TestTable;", function(err,rowCount,rows) {
        if(err)            
        {
            console.log("errors is " +err);
        }else{console.log(rowCount + ' rows');}
        connection.close();
        });
        var result = "";
        request.on('row', function(columns) {  
            columns.forEach(function(column) {  
              if (column.value === null) {  
                console.log('NULL');  
              } else {  
                result+= column.value + " ";  
              }  
            });  
            console.log(result);  
            result ="";  
        });  

        request.on('done', function(rowCount, more) {  
        console.log(rowCount + ' rows returned');  
        });  
        connection.execSql(request);  

    }

来源:https://stackoverflow.com/questions/58403493/requests-can-only-be-made-in-the-loggedin-state-not-the-connecting-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!