Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mapper.map throw error Mapping not found #249

Closed
zieglar opened this issue Jan 28, 2021 · 7 comments
Closed

Use mapper.map throw error Mapping not found #249

zieglar opened this issue Jan 28, 2021 · 7 comments
Labels
bug Something isn't working question Further information is requested

Comments

@zieglar
Copy link

zieglar commented Jan 28, 2021

Is your feature request related to a problem? Please describe.
I create MappingProfile with DoctorEntity and DoctorInfoDto in nestjs project
When I use this.mapper.mapArray(records, DoctorInfoDto, DoctorEntity)
result is ok,
But When I use this.mapper.map(record, DoctorInfoDto, DoctorEntity)
throw error Mapping not found for source function Array() { [native code] } and destination function Array() { [native code] }

TypeError: mapping is not iterable
    at map (/Users/zieglar/Code/project/node_modules/@automapper/core/src/lib/map/map.js:99:85)
    at mapReturn (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:68:12)
    at mapArray (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:199:31)
    at map (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:152:27)
    at Object.mapReturn (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:68:12)
    at Object.map (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/create-mapper/create-mapper.js:48:30)
    at DoctorV1Service.buildDoctorDetail (/Users/zieglar/Code/jkys-api/dist/api/v1/doctor-v1.service.js:143:33)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async DoctorV1Controller.getDoctorDetail (/Users/zieglar/Code/jkys-api/dist/api/v1/doctor-v1.controller.js:56:16)

use this.mapper.mapArray([record], DoctorInfoDto, DoctorEntity) is ok...

Describe the solution you'd like
fix this error

@nartc
Copy link
Owner

nartc commented Jan 28, 2021

Hi @zieglar , can you please provide DoctorInfo and DoctorInfoDto models? It’d be awesome if you could provide a simple reproduce repo as well. Thanks

@zieglar
Copy link
Author

zieglar commented Jan 28, 2021

@ChildEntity('doctor')
export class DoctorEntity extends EmployeeEntity {
  @AutoMap()
  @Column({ default: '' })
  expert: string;

  @AutoMap()
  @Column({ type: 'simple-array', default: [] })
  titleTags: string[];

  @Column({ type: 'simple-array', default: [] })
  serviceTags: string[];

  @Column({ default: '' })
  description: string;

  @AutoMap()
  @Column({ default: 0 })
  totalPatients: number;

  @AutoMap()
  @Column({ default: 0, type: 'float' })
  ratingRate: number;

  constructor(item?: Partial<DoctorEntity>) {
    super();
    if (item) Object.assign(this, item);
  }
}
export class EmployeeEntity extends BaseUIDWithEnabledEntity {
  @AutoMap()
  @Column({ length: 50 })
  name: string;

  @AutoMap()
  @Column({
    enum: GenderType,
    default: GenderType.MALE,
    type: 'enum',
  })
  gender: GenderType;

  @AutoMap()
  @Column({ default: '', length: 200 })
  avatar: string;
}
export class DoctorInfoDto {
  @AutoMap()
  id: string;

  @AutoMap()
  name: string;

  rank: string;

  @AutoMap()
  avatar: string;

  @AutoMap()
  titleTags: string[];

  departmentName: string;

  hospitalName: string;

  @AutoMap()
  expert: string;

  serviceTags: ServiceTag[];

  @AutoMap()
  totalPatients: number;

  @AutoMap()
  ratingRate: number;

  price: number;
}
      mapper
        .createMap(DoctorEntity, DoctorInfoDto)
        .forMember((d) => d.departmentName, fromValue('null'))
        .forMember((d) => d.hospitalName, fromValue('null'))
        .forMember(
          (d) => d.rank,
          mapFrom((s) => DoctorRankName.get(s.rank)),
        )
        .forMember(
          (d) => d.serviceTags,
          mapFrom((s) =>
            s.serviceTags.map((tag) => {
              return {
                name: BaseServiceNames.get(tag),
                ...BaseServiceConf.get(tag),
              };
            }),
          ),
        );

@nartc
Copy link
Owner

nartc commented Jan 28, 2021

The stacktrace

TypeError: mapping is not iterable
    at map (/Users/zieglar/Code/project/node_modules/@automapper/core/src/lib/map/map.js:99:85)
    at mapReturn (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:68:12)
    at mapArray (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:199:31)
    at map (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:152:27)

tells me that it errors out at this line:

setMember(mapArray(mapInitializedValue, nestedDestinationMemberKey, nestedSourceMemberKey, undefined, mapper, errorHandler));

which means that Automapper is trying to map an Array of Object that wasn't configured with forMember() (hint: mapInitializedValue). Looking at the models you provide, I don't see any properties that have a type of SomeModel[]. So, can I also have a sample record (that you used to produce this bug) as the source object to map?

PS: I can also see that the error message isn't that helpful which I can try to make that better in the next release.

@nartc nartc added bug Something isn't working question Further information is requested labels Jan 28, 2021
@zieglar
Copy link
Author

zieglar commented Jan 28, 2021

DoctorEntity {
  id: '1e91d2be-e191-41ac-a0ad-83cc77283f5c',
  createdAt: '2021-01-21 15:45:04',
  updatedAt: '2021-01-21 15:45:04',
  enabled: true,
  name: 'aaa',
  gender: 'male',
  mobilePhone: '11111111111',
  username: '11111111111',
  idNumber: null,
  isRealNameAuth: false,
  avatar: '',
  birthday: '',
  age: 48,
  middleStageUserId: null,
  type: 'doctor',
  level: '',
  expert: 'abc',
  rank: 'deputyChiefPhysician',
  titleTags: [ 'aaa', 'ccc' ],
  serviceTags: [ 'aa', 'vv', 'cc' ],
  description: 'description',
  totalPatients: 0,
  ratingRate: 0,
  recommendedRating: 100,
  index: 1,
  showOnIndex: true
}

@zieglar
Copy link
Author

zieglar commented Jan 28, 2021

The stacktrace

TypeError: mapping is not iterable
    at map (/Users/zieglar/Code/project/node_modules/@automapper/core/src/lib/map/map.js:99:85)
    at mapReturn (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:68:12)
    at mapArray (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:199:31)
    at map (/Users/zieglar/Code/jkys-api/node_modules/@automapper/core/src/lib/map/map.js:152:27)

tells me that it errors out at this line:

setMember(mapArray(mapInitializedValue, nestedDestinationMemberKey, nestedSourceMemberKey, undefined, mapper, errorHandler));

which means that Automapper is trying to map an Array of Object that wasn't configured with forMember() (hint: mapInitializedValue). Looking at the models you provide, I don't see any properties that have a type of SomeModel[]. So, can I also have a sample record (that you used to produce this bug) as the source object to map?

PS: I can also see that the error message isn't that helpful which I can try to make that better in the next release.

But why mapArray can map it?

@nartc
Copy link
Owner

nartc commented Jan 28, 2021

Are you available for a video call so I can take a closer look at your current implementation? DoctorEntity does not look complicated at all so it should map fine. I've no idea why map does not work and mapArray works.

@zieglar
Copy link
Author

zieglar commented Jan 28, 2021

send you mail :)

@nartc nartc closed this as completed in c5d111d Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants