NODE.JS 'Connectionerror with MSSQL

Asked

Viewed 742 times

-1

It’s the first time I’ve worked with javascript and Node.js.

I have no idea what the error might be or how to make the connection, my problem initially arose here: https://stackoverflow.com/questions/52951825/getting-information-from-a-json-and-saving-in-sql-server-using-javascript.

I have the following code, simple that connects a server Node in a database and makes a select only.

    var express = require('express');
var app = express();

app.get('/', function (req, res) {

    var sql = require("mssql");

    // config for your database
    var config = {
        user: 'xxxxxxx',
        password: 'xxxxxxx',
        server: 'devsqlcl2:1433', 
        database: 'xxxxx',
        port: "1433",
        dialect:",ssql",
        dialectOptiond:"SQLEXPRESS"
    };

    // connect to your database
    sql.connect(config, function (err) {

        if (err) console.log(err);

        // create Request object
        var request = new sql.Request();

        // query to the database and get the records
        request.query('select * from dbo.balance_papercut', function (err, recordset) {

            if (err) console.log(err)

            // send records as a response
            res.send(recordset);

        });
    });
});

var server = app.listen(5000, function () {
    console.log('Server is running..');
});

Now I have evolved to that part of Node.js, but I remain 100% lost on how to connect to the bank, however I am facing the following error below.

I have no idea what it could be. Connection to the bank should not be a problem, as I use the same credentials to access through a java system.

C:\Users\roberto.pannain\Documents\WIDS_REST_Example>node server.js
Server is running..
tedious deprecated The default value for `options.encrypt` will change from `false` to `true`. Please pass `false` explicitly if you want to retain current behaviour. node_modules\mssql\lib\tedious.js:230:23
tedious deprecated Passing non-number values for options.port will throw an error in future tedious versions. Please pass a number instead. node_modules\mssql\lib\tedious.js:230:23
{ ConnectionError: Failed to connect to devsqlcl2:1433:1433 - getaddrinfo ENOTFOUND devsqlcl2:1433
    at Connection.tedious.once.err (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\mssql\lib\tedious.js:237:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Connection.socketError (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\tedious\lib\connection.js:1024:14)
    at C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\tedious\lib\connection.js:868:25
    at GetAddrInfoReqWrap.callback (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\tedious\lib\connector.js:69:18)
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:79:17)
  code: 'ESOCKET',
  originalError:
   { ConnectionError: Failed to connect to devsqlcl2:1433:1433 - getaddrinfo ENOTFOUND devsqlcl2:1433
    at ConnectionError (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\tedious\lib\errors.js:12:12)
    at Connection.socketError (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\tedious\lib\connection.js:1024:30)
    at C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\tedious\lib\connection.js:868:25
    at GetAddrInfoReqWrap.callback (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\tedious\lib\connector.js:69:18)
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:79:17)
     message: 'Failed to connect to devsqlcl2:1433:1433 - getaddrinfo ENOTFOUND devsqlcl2:1433',
     code: 'ESOCKET' },
  name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
    at Request._query (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\mssql\lib\base.js:1330:37)
    at Request._query (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\mssql\lib\tedious.js:526:11)
    at Request.query (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\mssql\lib\base.js:1266:12)
    at C:\Users\roberto.pannain\Documents\WIDS_REST_Example\server.js:28:17
    at _poolCreate.then.catch.err (C:\Users\roberto.pannain\Documents\WIDS_REST_Example\node_modules\mssql\lib\base.js:275:7)
    at <anonymous> code: 'ECONNCLOSED', name: 'ConnectionError' }
  • Good afternoon, the main error is the var = config is not correct, in the server you put localhost// nome da instancia, without the value of the port. The value of the port goes in its respective variable. If you are mounting the string with the values in it, I advise you to do so sql.connect('mssql://username:password@localhost/database').

  • For more than one read on these two sites here Luiztools mssql Node , github mssql and SQL Server in Node.js

  • @Andersonmendes Thank you very much!!!! It worked the way you advised!

  • Oops good, I’ll post as an answer then.

1 answer

0


As discussed in the comments, problem in connection string. Correct format of connection string.

var config = {
    user: 'username',
    password: 'password',
    server: 'localhost/instancia', 
    database: 'database',
    port: '1111',
    dialect:",ssql",
    dialectOptiond:"SQLEXPRESS"
};
// connect to your database
sql.connect(config, function (err) {

    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query('select * from dbo.balance_papercut', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

    });
});

or

// connect to your database
sql.connect('mssql://username:password@localhost/database', function (err) {

    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query('select * from dbo.balance_papercut', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

    });
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.