简体   繁体   English

尝试执行批量插入时出现 node-mssql 错误消息(从主机读取当前行时,遇到过早的消息结束)

[英]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.

相关问题 使用Node-mssql与Azure sql数据库连接时出现连接问题 - Having issues with connection when using node-mssql connected with Azure sql database 从 Angular 8 中的 http 响应读取错误状态代码时“无法读取 null 的属性消息” - "Cannot read property message of null" when reading error status code from http response in Angular 8 过滤时找不到数据时如何显示错误消息 - How to show an error message when no data is found while filtering 打字稿2.7.2:无法阅读此错误消息 - Typescript 2.7.2: Trouble reading this error message Angular 4奇怪的错误:-不显示消息吐司时查询没有结束 - Angular 4 weird error: query doesn't end when -not- showing a message toast 尝试使用 typeorm(节点)进行迁移时出现目录错误 - Directory error when trying to do migrations using typeorm (node) 不从减速器返回 state 时出现神秘的 TypeScript 错误消息 - cryptic TypeScript error message when not returning the state from a reducer 访问 Typescript-ES5 或节点错误消息 - Accessing Typescript-ES5 or Node Error message 如何显示从API到最终用户的错误消息? - How can I show error message from API to the end-user? 在 typescript 中使用 Generic 时出现错误消息 - Getting error message while using Generic in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM