SQL Server Request Data from MSSQL with Node.js -- Error

knpiaxh1  于 2023-05-28  发布在  Node.js
关注(0)|答案(3)|浏览(234)

I want to connect and Request from MSSQL using nodejs to link it with magento. I Am trying to fix it for days now but it ends on the same way...

This is my Error Code:

Connected
{ RequestError: Requests can only be made in the LoggedIn state, not the Connecting state
at RequestError (C:\Workspace\Visual-Code\nodeApi\node_modules\tedious\lib\errors.js:34:12)
at Connection.makeRequest (C:\Workspace\Visual-Code\nodeApi\node_modules\tedious\lib\connection.js:1423:33)
at Connection.execSql (C:\Workspace\Visual-Code\nodeApi\node_modules\tedious\lib\connection.js:1194:19)
at executeStatement (C:\Workspace\Visual-Code\nodeApi\nodeapi.js:41:20)
at Connection.<anonymous> (C:\Workspace\Visual-Code\nodeApi\nodeapi.js:14:9)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.socketError (C:\Workspace\Visual-Code\nodeApi\node_modules\tedious\lib\connection.js:869:14)
at C:\Workspace\Visual-Code\nodeApi\node_modules\tedious\lib\connection.js:739:25
at GetAddrInfoReqWrap.callback (C:\Workspace\Visual-Code\nodeApi\node_modules\tedious\lib\connector.js:68:18)
message: 'Requests can only be made in the LoggedIn state, not the 
Connecting state',
code: 'EINVALIDSTATE' }

I searched a lot and found similar problems but nothing solved it... This is my Code maybe you can help me Spot the mistake.

var Connection = require('tedious').Connection;  
var config = {  
    userName: 'Cool userName',  
    password: 'awesome password',  
    server: 'amazing server',
    options: {
        database: 'database',
    }  
};  
var connection = new Connection(config);  
connection.on('connect', function(err) {  
    // If no error, then good to proceed.  
    console.log("Connected");  
    executeStatement();  
});  

var Request = require('tedious').Request;  
var TYPES = require('tedious').TYPES;  

function executeStatement() {  
    request = new Request("select * from Artikelstamm;", function(err) {  
    if (err) {  
        console.log(err);}  
    });  
    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);
}
xuo3flqw

xuo3flqw1#

Set Firewall rule to your DB by adding CleintIP. In Azure SQLDB, there is SET FIREWALL RULE button. You can use it to add IP.

klr1opcd

klr1opcd2#

I don't see code to initialize the connection anywhere in that sample:

connection.connect();

This is demonstrated at the bottom of the very first "getting started" example in the official documentation .

xwmevbvl

xwmevbvl3#

Add this event to your request

request.on("requestCompleted", function () {
    connection.close();
    resolve(result);
});

相关问题