简体   繁体   中英

I created REST API for SQL SERVER to get the all the record using node.js

Below is code of Node.js for getting the data from SQL server but it give an error "Global connection already exists. Call sql.close() first."

var express = require('express');
var app = express();
app.get('/', function (req, res) {
    var sql = require("mssql");
    var config = {
        user: 'sa',
        password: '',
        server: '',
        database: 'Test' 
    };
    sql.connect(config, function (err) {
        if (err) console.log(err);
        var request = new sql.Request();
        request.query('select * from TestTable', function (err, recordset) {
            if (err) console.log(err)
            res.send(recordset);
        });
    });
});
var server = app.listen(5000, function () {
    console.log('Server is running..');
});

您应该添加sql.close()后您的代码sql.connect()它应该工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM