I'm using NestJs with https://github.com/nartc/mapper (automapper).
I really love this library but it doesn't come with structured documentation for NestJs, so a lot of errors aren't documented.
In my others entities it just works and I'm doing the same thing
when I create this map:
createMap(mapper, CreateUserDto, User);
I'm getting this error:
TypeError: Cannot convert undefined or null to object
please help me
here's my DTO and my entity:
DTO:
import { IsArray, IsEmail, IsEnum, IsNotEmpty, IsNumber, IsOptional, IsString, ValidateNested } from "class-validator";
import { Type } from "class-transformer";
import { Scope } from "src/scope/scope.enum";
import { Status } from "src/status/status.enum";
import { ReadDepartmentDto } from "src/department/dept.dto";
import { ReadProjectDto } from "src/project/project.dto";
import { AutoMap } from "@automapper/classes";
export class CreateUserDto {
@IsNumber()
@AutoMap()
department!: number;
@IsOptional()
@IsArray()
@ValidateNested({ each: true})
@IsNumber()
@AutoMap()
isRefBusinessOf?: number[];
@IsOptional()
@IsArray()
@ValidateNested({ each: true})
@IsNumber()
@AutoMap()
isProjectManagerOf?: number[];
@IsEmail()
@AutoMap()
email!: string;
@IsString()
@AutoMap()
firstname!: string;
@IsString()
@AutoMap()
lastname!: string;
@IsOptional()
@IsString()
@AutoMap()
avatarUrl?: string;
@IsEnum(Scope, { each: true })
@AutoMap()
scopes!: Scope[];
@IsEnum(Status, { each: true })
@AutoMap()
statuses!: Status[];
@IsNotEmpty()
@AutoMap()
password!: string;
}
entity:
import { Scope } from "src/scope/scope.enum";
import { Exclude, instanceToPlain } from "class-transformer";
import { Department } from "src/department/dept.entity";
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany } from "typeorm";
import { Project } from "src/project/project.entity";
import { Status } from "src/status/status.enum";
import { AutoMap } from "@automapper/classes";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id?: number;
@AutoMap(() => Department)
@ManyToOne(() => Department, (department: Department) => department.users, { nullable: true })
department!: Department;
@AutoMap(() => [Project])
@OneToMany(() => Project, (project: Project) => project.projectManager, { nullable: true, onDelete: 'SET NULL' })
isProjectManagerOf?: Project[];
@AutoMap(() => [Project])
@OneToMany(() => Project, (project: Project) => project, { nullable: true, onDelete: 'SET NULL' })
isRefBusinessOf?: Project[];
@AutoMap()
@Column({ unique: true })
email!: string;
@AutoMap()
@Column()
firstname!: string;
@AutoMap()
@Column()
lastname!: string;
@AutoMap()
@Column({ nullable: true })
avatarUrl?: string;
@AutoMap()
@Column({
type: "enum",
enum: Scope,
default: [Scope.Admin],
array: true,
})
scopes!: Scope[];
@AutoMap()
@Column({
type: 'enum',
enum: Status,
default: [Status.Active],
array: true,
})
statuses!: Status[]
@Column({ nullable: true })
@Exclude({ toPlainOnly: true })
passwordSalt?: string;
@Column({ nullable: true })
@Exclude({ toPlainOnly: true })
rtrs?: string;
@Column({ nullable: true })
@Exclude({ toPlainOnly: true })
passwordHash?: string;
toJSON() {
return instanceToPlain(this);
}
}