【问题标题】:How to connect to mssql server synchronously in node.jsnode.js中如何同步连接mssql服务器
【发布时间】:2019-08-30 19:33:19
【问题描述】:

所有使用 mssql 客户端包/繁琐驱动程序的示例都是针对异步/回调/承诺的,但我只是在开发一个使用有限的微服务,而且我对异步函数的理解仍然有点模糊。 这是我尝试使用 async/await 的原因:

报告生成类:

    const mssql = require('mssql');
    const events = require('events');
    class reporter {
        constructor(searcher, logger) {
            // Pass in search type and value or log the error of none defined
            this.lg = logger
            if (searcher.type && searcher.content) {
                this.lg.lg("reporter created", 3)
                this.srchType = searcher.type;
                this.srchContent = searcher.content;
            } else {
                this.lg.lg("!MISSING SEARCH PARAMETERS", 0);
                this.err = "!MISSING SEARCH PARAMETERS";
            }
        }
        proc() {
            //DB Connect async
            async () => {
                try {
                    await mssql.connect('mssql://username:password@localhost/database')
                    this.result = await mssql.query`select * from mytable where id = ${this.searcher}`
                } catch (err) {
                    // ... error checks
                }
            }
            return this.result;
        }

    }

然后调用:

    //Pass to reporter for resolution
    var report1 = new reporter(searcher, logs);

    report1.proc();

我确信这可能是实现这一目标的一种非常糟糕的方式,所以我也愿意接受任何关于实现最终目标的好方法的意见,但我仍然想知道是否可以同步完成.

【问题讨论】:

标签: javascript node.js database microservices node-modules


【解决方案1】:

一种更新的答案,继续 O. Jones 的答案。 当前版本的 Node.js (v15+) 支持顶级等待,这意味着您可以按顺序运行它。

import mssql from 'mssql';
await mssql.connect('mssql://username:password@localhost/database')
const result = await mssql.query`select * from mytable where id = ${this.searcher}`

但仍应避免这种情况,因为您想捕获错误而不是让它崩溃。

在当前版本的 Node.js 中,如果 await/promise 被拒绝,并且没有被 .catch() 捕获,那么未捕获的 Promise 将终止您的应用程序并显示错误

【讨论】:

    【解决方案2】:

    您不能同步进行。弄清楚这些异步的东西绝对值得您花时间和精力。

    async / await / promises 让你或多或少地假装同步

    const report1 = new reporter(searcher, logs);
    report1.proc()
    .then ( result => {
        /* in this function, "result" is what your async function returned */
        /* do res.send() here if you're in express */
    } )
    .catch ( error => {
        /* your lookup failed */
        /* inform the client of your web service about the failure
         * in an appropriate way. */
    } )
    

    并且,在你的 proc 函数中解开异步函数,如下所示:

        async proc() {
                try {
                    await mssql.connect('mssql://username:password@localhost/database')
                    this.result = await mssql.query`select * from mytable where id = ${this.searcher}`
                } catch (err) {
                    // ... error checks
                }
            return this.result;
        }
    

    await.then 是类似的。

    【讨论】:

    • 哦,捂脸,抱歉,这是then() 而不是next()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2012-07-22
    • 2014-01-29
    • 1970-01-01
    相关资源
    最近更新 更多