MSSQL执行:返回Promise和Recordsets
我正在使用Node-MSSQL
在事务内执行一堆操作。 Spceweye,按顺序使用的方法为开始tran
,bulk
,bulk
,exec
,commits < /代码>。 Here is part of the code, which works as-is:
// bulk insert #columns
.then(result => {
const request=new sql.Request(globalTx);
const tableObj = new sql.Table('#columns'); //#columns
/* some code to build the desired tableObj*/
return request.bulk(tableObj);
})
// exec proc
.then(result => {
returnObject.lastSuccessfulStep='bulk #data'
returnObject["#columns rows"]=result.rowsAffected
const request=new sql.Request(globalTx);
/* some code to build the desired proc call*/
return request.execute('spc_sqlDMLCommand');
})
//commit
.then(result => {
return globalTx.commit(); // returns promise
})
So you can see the use of chained .then
s.最后有一个catch()
。
我的问题是,在执行
方法的情况下通过其回调返回。引用有关第二个参数的参考:
回调(err,记录集,returnValue) - 称为回调 执行完成或发生错误后。 returnValue 也可以作为记录集的属性访问。选修的。如果省略, 返回承诺。
是否有一种方法可以将ERR/RECORKET/RETURNVALUE保存到某些对象,同时仍保持流量,即不必将后续剪切到
s中 s ececute < /code>的回调?
解决方案尝试:
我尝试使用回调:
request.execute('spc_sqlDMLCommand',function cb(a,b,c){
console.log('a:\n',a,'b:\n',b,'c:\n',c)
});
但是我得到了此错误:
消息:'请求只能在loggedin状态下提出,而不是 sendclientRequest状态',代码:'einvalidstate'
之后,在Internet上添加了此之后,它似乎是关于有问题的多个查询。但是,如果我以上面提到的方式进行操作,它已经对我有用...
I am using node-mssql
to perform a bunch of operations inside a transaction. Spcifically, the methods used in order are begin tran
,bulk
,bulk
,exec
,commit
. Here is part of the code, which works as-is:
// bulk insert #columns
.then(result => {
const request=new sql.Request(globalTx);
const tableObj = new sql.Table('#columns'); //#columns
/* some code to build the desired tableObj*/
return request.bulk(tableObj);
})
// exec proc
.then(result => {
returnObject.lastSuccessfulStep='bulk #data'
returnObject["#columns rows"]=result.rowsAffected
const request=new sql.Request(globalTx);
/* some code to build the desired proc call*/
return request.execute('spc_sqlDMLCommand');
})
//commit
.then(result => {
return globalTx.commit(); // returns promise
})
So you can see the use of chained .then
s. There is a catch()
at the end.
My problem is that in the case od the execute
method, while the then() /catch()
promise structure feels friendly to me, I also need the information that the method returns through its callback. Citing the reference about the second parameter:
callback(err, recordsets, returnValue) - A callback which is called
after execution has completed, or an error has occurred. returnValue
is also accessible as property of recordsets. Optional. If omitted,
returns Promise.
Is there a way to save err/recordsets/returnValue to some objects while still maintaining the flow, ie not having to cut-paste the subsequent then()
s into the execute
's callback?
Solution attempt:
I tried using a callback:
request.execute('spc_sqlDMLCommand',function cb(a,b,c){
console.log('a:\n',a,'b:\n',b,'c:\n',c)
});
but I got this error:
message: 'Requests can only be made in the LoggedIn state, not the
SentClientRequest state', code: 'EINVALIDSTATE'
Following this up on the internet, it seems to be about multiple queries having problems. But, it already works for me if I do it in the way mentioned above...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论