尝试通过 Typeorm 将值插入 Mssql 数据库时出现 "ERR_INVALID_ARG_TYPE" 错误

Getting "ERR_INVALID_ARG_TYPE" error while trying to insert values in to Mssql database through Typeorm

我正在为一个项目使用 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'
}

请让我知道哪里出错了。

更改您的 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 class 中的注释:

@ApiProperty({ type: 'number' })

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

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

参考下面link解决

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