如何解析Node.js mssql承诺

如何解析Node.js mssql承诺,node.js,sql-server,promise,es6-promise,tedious,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(re

我试图使用来自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
超时可能与

弃用警告:由于安全性和可用性问题,Buffer()已弃用。请改用Buffer.alloc()、Buffer.allocUnsafe()或Buffer.from()方法


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

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

在第三个例子中。您的输入在execute之后,所以这可能是导致错误的原因

当一个请求返回一个承诺时,您可以像您已经做的那样将它们与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返回,并且无论如何只需要一个请求。缺点是,您可能需要更专门的过程。

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

在第三个例子中。您的输入在execute之后,所以这可能是导致错误的原因

当一个请求返回一个承诺时,您可以像您已经做的那样将它们与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返回,并且无论如何只需要一个请求。缺点是,您可能需要更专门的程序。

您是否使用
mssql@3.3.0
?正如我所看到的,最新版本是
5.1.0
,alpha是
6.0.0-alpha.9
是的,一些
4.x
5.1.0
版本由于与IISNode的一些冲突而崩溃,我还没有弄清楚。我可以尝试
alpha
,但到目前为止测试还没有希望……所以,到目前为止,您尝试了什么?仍然得到
(节点:7132)[DEP0005]弃用警告:由于安全性和可用性问题,Buffer()已弃用。请改用Buffer.alloc()、Buffer.allocUnsafe()或Buffer.from()方法。
升级到
6.0.0-alpha.9
后是否使用
mssql@3.3.0
?正如我所看到的,最新版本是
5.1.0
,alpha是
6.0.0-alpha.9
是的,一些
4.x
5.1.0
版本由于与IISNode的一些冲突而崩溃,我还没有弄清楚。我可以尝试
alpha
,但到目前为止测试还没有希望……所以,到目前为止,您尝试了什么?仍然得到
(节点:7132)[DEP0005]弃用警告:由于安全性和可用性问题,Buffer()已弃用。请改用Buffer.alloc()、Buffer.allocUnsafe()或Buffer.from()方法。
升级到
6.0.0-alpha.9