使用Node.js从MSSQL请求数据 - 错误

时间:2018-01-24 12:38:46

标签: sql sql-server node.js request connect

我想使用nodejs连接和请求MSSQL将其与magento链接。 我现在试图解决它几天但它以同样的方式结束......

这是我的错误代码:

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' }

我搜索了很多,发现了类似的问题,但没有解决它...... 这是我的代码也许你可以帮助我发现错误。

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

1 个答案:

答案 0 :(得分:0)

通过添加CleintIP将防火墙规则设置为您的数据库。在Azure SQLDB中,有“设置防火墙规则”按钮。您可以使用它来添加IP。