无法使Node mssql正常工作 [英] Can't get Node mssql to work properly

查看:154
本文介绍了无法使Node mssql正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前使用mssql的方式,但有时会出现错误:

This is the way I'm using mssql at the moment but it gives sometimes errors:

JavaScript:

JavaScript:

router.get('/academiejaren', (req, res) => {
    sql.connect(dbconfig, function (err) {
        var request = new sql.Request();
        if (err) {
            console.log(err);
            return;
        }
        request.query("SELECT * FROM [Alg].[DefAJ];", function (err, recordset) {
            if (err) {
                console.log(err);
                return;
            }
            else {
                res.end(JSON.stringify(recordset));
            }
        });
        request.query();
    });
});

错误:

{ ConnectionError: Connection is closed.
    at C:\Users\Milan\Documents\Octopus\Octopus 2.0\node_modules\mssql\lib\main.js:1569:17
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
  name: 'ConnectionError',
  message: 'Connection is closed.',
  code: 'ECONNCLOSED' }
(node:556) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ConnectionError: Connection is closed.
(node:556) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我使用的是3.2.0版,因为我无法使用最新的4.0.2版...任何帮助或一些好的示例,因为我无法弄清楚文档...

I'm using version 3.2.0 because I can't get the newest one, 4.0.2, to work... Any help or some good examples, because I can't figure out the documentation...

先谢谢!

编辑

在一个小型测试项目上更新为4.0.2.然后我开始工作了.要将我的API更改为此更新.

Updated on a small test project to 4.0.2. and I got it to work. Going to change my API to this update.

router.get('/academiejaren', (req, res) => {
    (async function () {
        try {
            let pool = await sql.connect(config)
            let result1 = await pool.request()
                .query('SELECT * FROM [Alg].[DefAJ];')

            res.send(JSON.stringify(result1.recordset));


        } catch (err) {
            res.send("CAUGHT ERROR academiejaren");
        }
    })()

    sql.on('error', err => {
        // ... error handler
    })
});

现在我还有一个小问题,该如何处理catch和sql.on()?我应该如何处理错误?

Now I do have a small question left, what should I do with the catch and sql.on()? How should I handle error's?

推荐答案

router.get('/academiejaren', (req, res) => {
    sql.connect(dbconfig, function (err) {
        var request = new sql.Request();
        if (err) {
            console.log(err);
            return;
        }
        request.query("SELECT * FROM [Alg].[DefAJ];", function (err, recordset) {
            if (err) {
                console.log(err);
                return;
            }
            else {
                res.send(JSON.stringify(recordset));
            }
        });
        request.query();
    });
});

您确实做了res.endend退出了它,想要res.send

you literally did res.end, end exits it, you want res.send

这篇关于无法使Node mssql正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆