It took me some time to find the reasons for the connection error below:
Requests can only be made in the LoggedIn state, n…", code: "EINVALIDSTATE"
I was trying to connect to MS Sql server using Node JS TEDIOUS package.
The code from the Microsoft is very simple and didn't return a more complete error message.
This is the original code:
...
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement(); });
...
Checking the internet a bit more, I found a way to get more insight into the error:
...
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) return console.error(err);
console.log("Connected");
executeStatement(); });
...
That raised:
ConnectionError {message: "Failed to connect to 10.0.0.100:1433 - connect …", code: "ESOCKET", stack: "ConnectionError: Failed to connect to 10.0.0.100...
Now I could investigate my connection configuration further. The template from the Microsoft page...
...
var config = { userName: 'yourusername', password: 'yourpassword', server: 'yourserver.database.windows.net',
// When you connect to Azure SQL Database, you need these next options.
options: {encrypt: true, database: 'your database'} };
...
was actually missing the instanceName option and once I added it, the connection was successful.
Requests can only be made in the LoggedIn state, n…", code: "EINVALIDSTATE"
I was trying to connect to MS Sql server using Node JS TEDIOUS package.
The code from the Microsoft is very simple and didn't return a more complete error message.
This is the original code:
...
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement(); });
...
Checking the internet a bit more, I found a way to get more insight into the error:
...
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) return console.error(err);
console.log("Connected");
executeStatement(); });
...
That raised:
ConnectionError {message: "Failed to connect to 10.0.0.100:1433 - connect …", code: "ESOCKET", stack: "ConnectionError: Failed to connect to 10.0.0.100...
Now I could investigate my connection configuration further. The template from the Microsoft page...
...
var config = { userName: 'yourusername', password: 'yourpassword', server: 'yourserver.database.windows.net',
// When you connect to Azure SQL Database, you need these next options.
options: {encrypt: true, database: 'your database'} };
...
was actually missing the instanceName option and once I added it, the connection was successful.
...
var Connection = require('tedious').Connection;
var config = {
userName: 'yourusername',
password: 'yourpassword',
server: 'yourserver',
options: {
instanceName: 'yourinstance',
database: 'yourdatabase',
debug: {
packet: false,
payload: false,
token: false,
data: false
}
}
}
...
Setting firewall rule by adding client IP in Azure SQLDB also solves the problem
ReplyDeleteThanks for your comment! I'll also check that option!
ReplyDelete