'Why I get no overload matches with typeorm and nest.js
Hey I'm trying to create an entity for task and use this entity,
so here is my task entity:
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
OneToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import { Status } from './Status.entity';
import { User } from './User.entity';
@Entity()
export class Task {
@PrimaryGeneratedColumn({
type: 'bigint',
name: 'task_id',
})
id: number;
@Column()
title: string;
@Column()
description: string;
@OneToOne(() => Status)
@JoinColumn()
status: Status;
@OneToOne(() => User)
@JoinColumn()
createdBy: User;
@OneToOne(() => User)
@JoinColumn()
updatedBy: User;
@CreateDateColumn({
type: 'timestamp',
name: 'created_at',
})
createdAt: Date;
@UpdateDateColumn({
type: 'timestamp',
name: 'updated_at',
})
updateAt: Date;
}
and I have everything else in the project already like the Status entity, and the user entity
when I'm trying to create a task like this
async insertTask(title: string, description: string, userId: number) {
const userDetails = await this.findUser(userId)
const newTask = this.TaskRepository.create({
title,
description,
createdBy: userDetails,
});
const res = await this.TaskRepository.save(newTask)
return res;
}
I get this error:
(node:5772) UnhandledPromiseRejectionWarning: QueryFailedError: Duplicate entry '8' for key 'task.REL_91d76dd2ae372b9b7dfb6bf3fd'
at Query.onResult (C:\Users\Kryolos.Hakeem\desktop\vodafone-tech-test\back-end\vodafone-test\src\driver\mysql\MysqlQueryRunner.ts:222:33)
at Query.execute (C:\Users\Kryolos.Hakeem\desktop\vodafone-tech-test\back-end\vodafone-test\node_modules\mysql2\lib\commands\command.js:36:14)
at PoolConnection.handlePacket (C:\Users\Kryolos.Hakeem\desktop\vodafone-tech-test\back-end\vodafone-test\node_modules\mysql2\lib\connection.js:456:32)
at PacketParser.onPacket (C:\Users\Kryolos.Hakeem\desktop\vodafone-tech-test\back-end\vodafone-test\node_modules\mysql2\lib\connection.js:85:12)
at PacketParser.executeStart (C:\Users\Kryolos.Hakeem\desktop\vodafone-tech-test\back-end\vodafone-test\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (C:\Users\Kryolos.Hakeem\desktop\vodafone-tech-test\back-end\vodafone-test\node_modules\mysql2\lib\connection.js:92:25)
at Socket.emit (events.js:375:28)
at addChunk (internal/streams/readable.js:290:12)
at readableAddChunk (internal/streams/readable.js:265:9)
at Socket.Readable.push (internal/streams/readable.js:204:10)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5772) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5772) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:5772) UnhandledPromiseRejectionWarning: NotFoundException: User Not Found
at TasksService.findUser (C:\Users\Kryolos.Hakeem\desktop\vodafone-tech-test\back-end\vodafone-test\src\tasks\tasks.service.ts:70:11)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at TasksService.insertTask (C:\Users\Kryolos.Hakeem\desktop\vodafone-tech-test\back-end\vodafone-test\src\tasks\tasks.service.ts:19:25)
(node:5772) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
I have been stuck with this for a long time now, any help would be appreciated, Thanks
Solution 1:[1]
I have a question, why are you saving the task twice? What is the difference between these two repository methods:
const newTask = this.TaskRepository.create
const res = await this.TaskRepository.save(newTask)
async insertTask(title: string, description: string, userId: number) {
const userDetails = await this.findUser(userId)
const newTask = this.TaskRepository.create({
title,
description,
createdBy: userDetails,
});
const res = await this.TaskRepository.save(newTask)
return res;
}
Solution 2:[2]
this is because updatedBy and createdBy fields on Task has the type User but you're supplying a number (the userId parameter).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mustapha Afkir |
| Solution 2 | Micael Levi |
