微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

创建地图时打字稿@automapper/core 错误

如何解决创建地图时打字稿@automapper/core 错误

Typescript 项目中的自动映射器有问题。在 documentation 之后,我收到一个错误,我不知道它为什么会发生以及如何解决它,根据它应该按原样工作的文档。它希望我初始化 User 对象,而在文档中没有初始化,当我这样做时,然后

没有与此调用匹配的过载。 重载 1 of 4,'(sourceObj: obj,destination: new (...args: unkNown[]) => UserDto,source: new (...args: unkNown[]) => obj,options?: MapOptions): UserDto',出现以下错误。 'typeof User' 类型的参数不能分配给类型 'new (...args: unkNown[]) => obj' 的参数。 构造签名的类型不兼容。 输入'new (id: Uuid,firstName: UserName,lastName: UserName,email: UserEmail,password?: UserPassword) => User' 不能分配到类型'new (...args: unkNown[]) => obj' . 类型 'User' 不能分配给类型 'obj'。 “用户”类型中缺少索引签名。 重载 2 of 4,destination: string,source: string,options?: MapOptions): 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,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,User); 似乎工作,但仍然存在初始化问题,错误 undefined name field must be provided 发生。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。