[英]node-mssql error message when trying to do a bulk insert (While reading current row from host, a premature end-of-message was encountered)
When trying to use the .bulk
operation of node-mssql
lib in node.js, i'm receiving the message:在 node.js 中尝试使用
node-mssql
lib 的.bulk
操作时,我收到消息:
While reading current row from host, a premature end-of-message was encountered--an incoming data stream was interrupted when the server expected to see more data. The host program may have terminated. Ensure that you are using a supported client application programming interface (API)
// the problem happens when i call this function
export async function insert(
transaction: sql.Request,
data: any[],
) {
const table = prepare(data);
return await transaction.bulk(table);
}
function prepare(data: any[]) {
const table = new sql.Table('dbo.SomeTable');
table.create = false;
table.columns.add('MyColumn1', sql.Int, { nullable: false });
table.columns.add('MyColumn2', sql.Int, { nullable: false });
table.columns.add('MyColumn3', sql.Int, { nullable: false });
for (const mov of data) {
table.rows.add(
mov.field1,
mov.field2,
mov.field3
);
}
return table;
}
After some time searching a solution, I discover that the problem was my data
array was empty.经过一段时间搜索解决方案后,我发现问题出在我的
data
数组是空的。 I found the solution in this link: https://github.com/tediousjs/tedious/issues/212#issuecomment-628330044我在这个链接中找到了解决方案: https://github.com/tediousjs/tedious/issues/212#issuecomment-628330044
I got a couple of this error before.
我之前遇到过几个这样的错误。 It happens also when you don't have any row inserted > in the loop.
当您没有在循环中插入任何行 > 时,也会发生这种情况。 A typical case is that you have columns added, and then you take some array > from other places, and iterate that array to insert row.
一个典型的情况是您添加了列,然后从其他地方获取一些数组 > 并迭代该数组以插入行。 Sometimes, when the array is empty, no row is inserted, and I'll get this error.
有时,当数组为空时,没有插入任何行,我会得到这个错误。 So, better to check if the array is empty before execute the bulkload.
因此,最好在执行批量加载之前检查数组是否为空。 (by user jianingliu , in Github)
(作者jianingliu ,Github)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.