Good day I have implemented the following logic:
router.get('/estudiante',(req,res,next) =>{
//res.send("hola mundo");
var sql = require("mssql");
// config for your database
var config = {
user: 'martin',
password: '1234',
server: 'JMTABORDA-PC\SQLEXPRESS',
database: 'school'
};
// 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 Student', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
By bringing it in via the URL, for example: link I am very good at listing all the students, but when I refresh the page again I get the following connection error:
Error: Global connection already exists. Call sql.close () first. at Object.connect (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ mssql \ lib \ base.js: 1591: 31) at router.get (C: \ Users \ marti \ Documents \ FinalProject \ src \ routes \ student.js: 23: 17) at Layer.handle [as handle_request] (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ layer.js: 95: 5) at next (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ route.js: 137: 13) at Route.dispatch (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ route.js: 112: 3) at Layer.handle [as handle_request] (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ layer.js: 95: 5) at C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ index.js: 281: 22 at Function.process_params (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ index.js: 335: 12) at next (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ index.js: 275: 10) at Function.handle (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ index.js: 174: 3)
I tried to implement a sql.Close (); but it does not appear to me.
I would appreciate if you can help me thank you very much