I am using Nestjs for a project, I am trying to insert values into Mssql database through Typeorm, I am getting the following error when I try to insert
TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received type number (0)
Below is the log while trying to insert
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":[]}]
Below is the code for inserting the data.
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;
}
Test Entity
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;
}
Error Log
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'
}
Please let me know where I am going wrong.
I was able to resolve this issue by removing the node_modules folder and force clear the NPM cache and finally reinstalling the node dependencies.
rm -rf node_modules
npm cache clear --force
npm install
referred below link to solve
https://peterthaleikis.com/posts/how-to-fix-throw-er-unhandled-error-event.html