Node js: mssql [ConnectionError: Connection is closed.] Name: 'ConnectionError', message: 'Connection is closed.', Code: 'ECONNCLOSED'

I get error in npm mssql 3.0.0 with sqlserver 2012

I am creating a single page application in which I used restful using express. There are 4 methods that execute a request and return data in response. for each method, I open the connection and close the connection.

but when calling storequery, a connection closure error occurs.

each method code is similar to the savedquery method (only those queries that are only inserted are copied), but they are executed. queryquery is not running

{[ConnectionError: connection closed.] Name: 'ConnectionError', message: "Connection closed", code: 'ECONNCLOSED'}

var savedquery=function(req,res){
       dbConfig= {
                user: 'XXX',
                password: 'XXXXXXXXXX',
                server: 'localhost', // You can use 'localhost\\instance' to connect to named instance 
                database: 'DEMO_ODS',       
                options: {
                    encrypt: true
                }
            };

        sql.connect(dbConfig).then(function (err) {
                var sqlrequest = new sql.Request();
                sqlrequest.query("SELECT * from SavedQuery").then(function (recordset) {
                    sql.close(function (value) {
                      console.log("connection6 closed");
                    });
                    return res.status(200).send(recordset);

                }).catch(function (err) {
                    console.log(err);
                });
            }).catch(function (err) { 
                console.log(err);
            });
        };
}
+4
2

, , , . , , promises, .

    function getData() {
    try {
        sqlInstance.connect(setUp)
            .then(function () {
                // Function to retrieve all the data - Start
                new sqlInstance.Request()
                    .query("select * from Course")
                    .then(function (dbData) {
                        if (dbData == null || dbData.length === 0)
                            return;
                        console.dir('All the courses');
                        console.dir(dbData);
                    })
                    .catch(function (error) {
                        console.dir(error);
                    });

                // Function to retrieve all the data - End

                // To retrieve specicfic data - Start
                var value = 1;
                new sqlInstance.Request()
                    .input("param", sqlInstance.Int, value)
                    .query("select * from Course where CourseID = @param")
                    .then(function (dbData) {
                        if (dbData == null || dbData.length === 0)
                            return;
                        console.dir('Course with ID = 1');
                        console.dir(dbData);
                    })
                    .catch(function (error) {
                        console.dir(error);
                    });
                // To retrieve specicfic data - End

            }).catch(function (error) {
                console.dir(error);
            });
    } catch (error) {
        console.dir(error);
    }
}

. .

+1

        options: {
            encrypt: true
        }

dbConfig

+2

Source: https://habr.com/ru/post/1626178/


All Articles