【问题标题】:Getting "ERR_INVALID_ARG_TYPE" error while trying to insert values in to Mssql database through Typeorm尝试通过 Typeorm 将值插入 Mssql 数据库时出现“ERR_INVALID_ARG_TYPE”错误
【发布时间】:2021-08-03 01:17:03
【问题描述】:

我在一个项目中使用 Nestjs,我试图通过 Typeorm 将值插入 Mssql 数据库,当我尝试插入时出现以下错误

TypeError [ERR_INVALID_ARG_TYPE]:“字符串”参数必须是字符串类型或 Buffer 或 ArrayBuffer 的实例。收到的类型号 (0)

下面是尝试插入时的日志

query: BEGIN TRANSACTION
query: DECLARE @OutputTable TABLE ("id" int);

INSERT INTO "test"("name", "code", "phone") OUTPUT INSERTED."id" INTO @OutputTable VALUES (@0, @1, @2);

SELECT * FROM @OutputTable -- PARAMETERS: [{"value":"aa","type":"nvarchar","params":[]}, 
{"value":"aa","type":"nvarchar","params":[]},{"value":"323","type":"nvarchar","params":[]}]

下面是插入数据的代码。

async create(createTestDto: CreateTestDto): Promise<TestEntity> {
const test = new TestEntity();
test.name = 'aa';
test.code = 'aa';
test.phone = '323';
await this.testRepository.save(test);
return test;
}

测试实体

import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
ObjectID,
Index,
PrimaryColumn,
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';

@Entity({ name: 'test' })
export class TestEntity {
constructor(data?: Partial<TestEntity>) {
Object.assign(this, data);
}

@ApiProperty({ type: 'string' })
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@Column()
code: string;

@Column()
phone: string;

}

错误日志

events.js:292
throw er; // Unhandled 'error' event
^

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type 
string or an instance of Buffer or ArrayBuffer. Received type number (0)
at Function.byteLength (buffer.js:728:11)
at RpcRequestPayload.generateParameterData (F:\Project\node_modules\tedious\lib\rpcrequest-payload.js:77:71)
at generateParameterData.next (<anonymous>)
at RpcRequestPayload.generateData (F:\Project\node_modules\tedious\lib\rpcrequest-payload.js:68:19) at generateData.next (<anonymous>)
at F:\Project\node_modules\tedious\node_modules\readable- 
stream\lib\internal\streams\from.js:43:35    
at Generator.next (<anonymous>)
at asyncGeneratorStep (F:\Project\node_modules\tedious\node_modules\readable-stream\lib\internal\streams\from.js:3:103)
at _next (F:\Project\node_modules\tedious\node_modules\readable- stream\lib\internal\streams\from.js:5:194)
at F:\Project\node_modules\tedious\node_modules\readable-stream\lib\internal\streams\from.js:5:364    
Emitted 'error' event on Readable instance at:
at emitErrorNT (F:\Project\node_modules\tedious\node_modules\readable- stream\lib\internal\streams\destroy.js:87:8)
at emitErrorAndCloseNT (F:\Project\node_modules\tedious\node_modules\readable-stream\lib\internal\streams\destroy.js:57:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'ERR_INVALID_ARG_TYPE'
}

请告诉我哪里出错了。

【问题讨论】:

  • 你有错误日志吗?
  • @ErangaHeshan,我已经用错误日志更新了我的问题。请检查
  • 你能不能也为TestEntity添加你的实体类?
  • @ErangaHeshan,我已经添加了TestEntity。

标签: node.js sql-server nestjs typeorm


【解决方案1】:

我能够通过删除 node_modules 文件夹并强制清除 NPM 缓存并最终重新安装节点依赖项来解决此问题。

rm -rf node_modules
npm cache clear --force
npm install

参考以下链接解决

https://peterthaleikis.com/posts/how-to-fix-throw-er-unhandled-error-event.html

【讨论】:

    【解决方案2】:

    如下更改您的 create 方法:

    async create(createTestDto: CreateTestDto): Promise<TestEntity> {
      const test = new TestEntity();
      test.name = 'aa';
      test.code = 'aa';
      test.phone = '323';
      return this.testRepository.save(test);
    }
    

    更改TestEntity 类中的注释:

    @ApiProperty({ type: 'number' })
    

    【讨论】:

    • 您是否重新构建了该应用程序?喜欢npm run build
    • 根据您的日志,插入操作似乎没问题。您能否检查数据库并验证您插入的数据是否在其中?
    • 当我尝试获取数据时会得到结果,当我尝试插入数据并通过 Id 获取数据时出现此错误。基本上当我们添加参数时。
    • 您的 API 调用是什么样的?
    • 我将它作为 json { "name": "CD", "code": "CD", "phone": "2323" } 发送
    猜你喜欢
    • 1970-01-01
    • 2020-11-19
    • 2012-04-04
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多