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

get undefined in output properties #139

Closed
mohammad-khooie opened this issue May 24, 2020 · 1 comment
Closed

get undefined in output properties #139

mohammad-khooie opened this issue May 24, 2020 · 1 comment

Comments

@mohammad-khooie
Copy link

mohammad-khooie commented May 24, 2020

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

at first thanks for your great library
I have a strange problem (get undefined in output properties)
The following steps are to reproduce:

@Entity()
class Country {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ nullable: false, unique: true })
  @AutoMap()
  name: string;

  @OneToMany(() => City, (city) => city.country)
  cities: City[];
}

@Entity()
class City {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ nullable: false })
  @AutoMap()
  name: string;

  @ManyToOne(() => Country, (country) => country.cities, { eager: true })
  country: Country;

  @OneToMany(() => Address, (address) => address.city)
  addresses: Address[];
}

@Entity()
class Address {
  @PrimaryGeneratedColumn('uuid')
  @AutoMap()
  id: string;

  @Column({ nullable: false })
  @AutoMap()
  descriptiveAddress: string;

  @Column({
    type: 'double precision',
    array: true,
    nullable: true,
    unique: true,
  })
  @AutoMap()
  coordinateAddress: number[];

  @Column({ nullable: true, unique: true })
  @AutoMap()
  postalCode: string;

  @ManyToOne(() => City, (city) => city.addresses, { eager: true })
  city: City;
}

class AddressRoDto {
  @AutoMap()
  id: string;

  @AutoMap()
  descriptiveAddress: string;

  @AutoMap()
  coordinateAddress: number[];

  @AutoMap()
  postalCode: string;

  @AutoMap()
  cityName: string;

  @AutoMap()
  countryName: string;
}

class AddressProfile extends ProfileBase {
  constructor(mapper: AutoMapper) {
    super();
    mapper
      .createMap(Address, AddressRoDto)
      .forMember(
        (d) => d.cityName,
        mapFrom((s) => s.city.name),
      )
      .forMember(
        (d) => d.countryName,
        mapFrom((s) => s.city.country.name),
      );
  }
}

class University {
  @PrimaryGeneratedColumn('uuid')
  @AutoMap()
  id: string;

  @Column({ nullable: true })
  @AutoMap()
  name: string;

  @OneToMany(() => Professor, (professor) => professor.univerity)
  @AutoMap(() => Professor)
  professors: Professor[];

  @OneToOne(() => Address)
  @JoinColumn()
  @AutoMap(() => Address)
  address: Address;
}

class UniversityRoDto {
  @AutoMap()
  id: string;

  @AutoMap()
  name: string;

  @AutoMap(() => AddressRoDto)
  address: AddressRoDto;

  @AutoMap(() => ProfessorRoDto)
  professors: ProfessorRoDto[];
}

@Profile()
class UniversityProfile extends ProfileBase {
  constructor(mapper: AutoMapper) {
    super();
    mapper.createMap(University, UniversityRoDto);
  }
}


class User {
  @PrimaryGeneratedColumn('uuid')
  @AutoMap()
  id: string;

  @Column({ nullable: false })
  @AutoMap()
  name: string;

  @Column({ nullable: true, unique: true })
  @AutoMap()
  email: string;

  @Column({ nullable: true })
  @AutoMap()
  emailConfirm: boolean;

  @Column({ nullable: true, unique: true })
  @AutoMap()
  phone: string;

  @Column({ nullable: false })
  @AutoMap()
  password: string;
}

class UserRoDto {
  @AutoMap()
  id: string;

  @AutoMap()
  name: string;

  @AutoMap()
  email: string;

  @AutoMap()
  phone: string;
}

@Profile()
class UserProfile extends ProfileBase {
  constructor(mapper: AutoMapper) {
    super();
    mapper.createMap(User, UserRoDto);
  }
}


@Entity()
class Professor {
  @PrimaryGeneratedColumn('uuid')
  @AutoMap()
  id: string;

  @Column({ nullable: false })
  @AutoMap()
  inUniversityAddress: string;

  @Column({ nullable: false })
  @AutoMap()
  grade: string;

  @OneToOne(() => User)
  @JoinColumn()
  @AutoMap(() => User)
  user: User;

  @ManyToOne(() => University, (university) => university.professors)
  @AutoMap(() => University)
  univerity: University;
}

class ProfessorRoDto {
  @AutoMap()
  id: string;

  @AutoMap(() => UserRoDto)
  user: UserRoDto;

  @AutoMap()
  grade: string;

  @AutoMap(() => UniversityRoDto)
  university: UniversityRoDto;

  @AutoMap()
  inUniversityAddress: string;
}

@Profile()
class ProfessorProfile extends ProfileBase {
  constructor(mapper: AutoMapper) {
    super();
    mapper.createMap(Professor, ProfessorRoDto);
  }
}

when I want to map Professor to ProfessorRoDto I get undefined properties in ProfessorRoDto .university although
when I map Professor.University to UniversityRoDto every thing is ok

console.log(this.mapper.map(professor, ProfessorRoDto, Professor)) log this result

ProfessorRoDto {
  id: '3374df70-60b0-43d8-8755-6735bbd55a2a',
  user: UserRoDto {
    id: 'd1e514fa-53d5-4405-b131-e46b621b9396',
    name: 'faridkh95',
    email: 'string95@gmail.com',
    phone: '09195877368'
  },
  grade: 'visiting_professor',
  university: UniversityRoDto {
    id: undefined,
    name: undefined,
    address: AddressRoDto {
      id: undefined,
      descriptiveAddress: undefined,
      coordinateAddress: [],
      postalCode: undefined,
      cityName: undefined,
      countryName: undefined
    }
  },
  inUniversityAddress: 'string'
} 

but console.log(this.mapper.map(university, UniversityRoDto, University)) log this result that is true

UniversityRoDto {
  id: '5fe20251-94c5-475f-a278-3428a21c0a5d',
  name: 'دانشگاه کاشان',
  address: null,
  professors: null
}

I user nestjs-automapper and every profiles loaded corectlly

Environment


node: 12.0.0
typesript:3.9.3
nestjs:7.0.13
 
@nartc
Copy link
Owner

nartc commented May 24, 2020

I think you misspelled university on Professor entity.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants