1

我对 Typescript 项目中的自动映射器有疑问。根据文档,我遇到了一个错误,我不知道它发生的原因以及如何解决它,基于它应该按原样工作的文档。它希望我在文档中没有初始化时初始化 User 对象,当我这样做时,然后

没有重载匹配此调用。重载 1 of 4, '(sourceObj: obj, destination: new (...args: unknown[]) => UserDto, source: new (...args: unknown[]) => obj, options?: MapOptions< obj, UserDto>): UserDto',给出以下错误。'typeof User' 类型的参数不能分配给'new (...args: unknown[]) => obj' 类型的参数。构造签名的类型不兼容。类型'new(id:Uuid,firstName:UserName,lastName:UserName,电子邮件:UserEmail,密码?:UserPassword)=> User'不可分配给类型'new(...args:unknown [])=> obj' . 类型“用户”不可分配给类型“obj”。“用户”类型中缺少索引签名。重载 2 of 4, '(sourceObj: obj, destination: string, source: string, options?: MapOptions<obj, UserDto>): UserDto',给出了以下错误。“typeof UserDto”类型的参数不能分配给“string”类型的参数。

课程:

@Entity('users')
export class User {

  @ObjectIdColumn()
  @AutoMap({ typeFn: () => Uuid })
  private _id: Uuid;

  @Column()
  @AutoMap({ typeFn: () => UserName })
  private firstName: UserName;

  @Column()
  @AutoMap({ typeFn: () => UserName })
  private lastName: UserName;

  @Column()
  @AutoMap({ typeFn: () => UserEmail })
  private email: UserEmail;

  @Column()
  @AutoMap({ typeFn: () => UserPassword })
  private password: UserPassword;
}

其中每个 refs 也有 AutoMap:

export class Uuid {
  @AutoMap()
  private value: string;
}

映射器本身:

import { createMapper } from '@automapper/core';
import { classes } from '@automapper/classes';
import { User } from '../User';
import { UserDto } from '../dto/UserDto';

export const mapper = createMapper({
  name: 'userMApper',
  pluginInitializer: classes,
});

mapper.createMap(User, UserDto);

最后发生错误的地方是我的 InMemoryHashMapDatabase:

findById(id: string): UserDto {
  const user = this.map.get(id);
  return mapper.map(user, UserDto, User);
}

带有初始化和对象返回方法 .toString()

findById(id: string): UserDto {
  const user = this.map.get(id);
  return mapper.map(
    user,
    UserDto,
    new User(
        new Uuid(user.id),
        new UserName(user.firstName, UserNameTypes.FIRST),
        new UserName(user.lastName, UserNameTypes.LAST),
        new UserEmail(user.email),
        new UserPassword(user.password),
      ).toString(),
    );

但后来我得到了另一个错误,因为每个类都有自己的验证

undefined name field must be provided
  15 |   constructor(name: string, type: UserNameTypes) {
  16 |     if (name == '' || name == null || name == undefined) {
> 17 |       throw new BadRequestException(`${type} name field must be provided`);
     |             ^
  18 |     }
  19 |
  20 |     if (name.length < UserName.minLength) {

  at new UserName (../src/user/domain/UserName.ts:17:13)
  at Object.instantiate (../packages/classes/src/lib/utils/instantiate.util.ts:122:11)
  at Object.instantiate (../packages/classes/src/lib/classes.ts:36:14)
  at Object.initializeMapping (../packages/classes/src/lib/classes.ts:66:64)
  at Object.createMap (../packages/core/src/lib/create-mapper/create-mapper.ts:38:30)
  at Object.<anonymous> (../src/user/domain/mapper/Mapper.ts:11:8)

听起来我必须在 Mapper.ts 文件中再次初始化 User 类,但没有输入。

我正在使用 NestJS 框架,但我不想通过模块集成它(所以没有 @InjectMapper() 之类的东西),我更喜欢使用它,因为它是一个典型的 ts 项目。

编辑:我已更改为 mapper.map() 函数中的参数类型

interface obj {
  [key: string]: string;
}

似乎可以正常any工作mapper.map(user, UserDto, User);,但是仍然存在发生错误的初始化undefined name field must be provided问题。

4

0 回答 0