node.js - 如何解析 Node.js mssql promise

标签 node.js sql-server promise es6-promise tedious

我正在尝试使用 SQL Server 存储过程的响应并将其传递到另一个存储过程以记录响应数据。

但是,我不确定如何将它们链接在一起,因为它们似乎都需要自己的连接池。

尝试1

// mssql@3.3.0

exports.chain = (req, res) => {
  sql.connect(config.properties).then(pool => {
    return pool.request()
      .execute("chain")
      .then(response => {
        return pool.request()
          .input("param", sql.NVarChar(300), result[0][0]["response"])
          .execute("chain2")
          .then(result => res.send(result))
          .catch(err => res.send(err))
      })
      .catch(err => res.send(err))
  })
}

// returns {}

尝试2

exports.chain = (req, res) => {
  sql.connect(config)
    .then(pool => {
      return pool.request()
        .execute("chain")
    }).then(result => {
      return pool.request()
        .input("param", sql.NVarChar(300), result[0][0]["response"])
        .execute("chain2")
    }).then(result => {
      res.send(result)
    }).catch(err => {
        // ... error checks
    })

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

// Throws error HTTP Status: 500, HTTP subStatus: 1013

尝试3

sql.connect(config.properties).then(pool => {
  return pool.request()
    .execute("chain")
  }).then(response => {
    pool.request()
      .execute("chain2")
      .input('param', sql.NVarChar(300), response[0][0]['response'])
      .then(response => res.send(response))
      .catch(err => res.send(err))
})

// Throws error HTTP Status: 500, HTTP subStatus: 1013

超时可能与

有关

DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

如何从第一个存储过程获取响应并将其传递到第二个存储过程进行日志记录?

最佳答案

连接池是一个由多个 TDS 连接组成的实例,因此您应该能够将多个过程连接到同一个池,因为新请求仅获取其中一个 TDS 连接。也许这是您的配置问题中的一个缺陷。

在你的第三个例子中。您的输入是在执行之后出现的,因此这可能会导致错误。

当请求返回一个 promise 时,您可以将它们与 then 链接起来,就像您已经在做的那样,但是每个 then 中的变量不在同一范围内,因此当您在 then 中处理不同的请求时,您无法访问从用户记录接收到的用户。

或者,如果您需要跨响应访问变量,您也可以通过回调嵌套它们:

sql.connect(config.properties).then(pool => {
    pool.request()
        .execute("chain", (error, response) => {
            const row = response.recordset;

            pool.request()
                .input('param', sql.NVarChar(300), row.foo)
                .execute("chain2", (err, res) => {
                    const row_ = res.recordset;

                    // send response to client
                });
        });
}).catch(err => {
    // error handling here
});

最佳解决方案可能是以 JSON 形式返回存储过程,而且无论如何您只需要一个请求。缺点是,您可能需要更专门的程序。

关于node.js - 如何解析 Node.js mssql promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56842930/

相关文章:

android - WebStorm + React-Native : Navigator is deprecated and has been removed from this package

javascript - 从函数定义中引用 GeneratorFunction 实例

javascript - 在 Sails.JS/Waterline 中聚合嵌套模型数据

javascript - 如何将 : Pass node. js mongodb 结果转换为 angularjs?

c# - 收到错误 : A network-related or instance-specific error occurred while establishing a connection to SQL Server

sql-server - 如何重命名我的约束

sql - 如何授予 Select 访问权限以在架构中查看

javascript - Nodejs 中的 Promise 使用 Q 将引用保存到 mongoose

javascript - NodeJS Promise 行为查询

c - OpenMP 支持异步操作吗?