Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
TypeORM results to undefined on OneToOne relationship

I'm new to NestJS (I had a bit of experience in Angular). I'm getting undefined when I console.log a JoinTable (the tables are new. I'm tasked to create a new microservice)

So Unit and Hub has One to One relationship.

Here is the unit

  @Entity({
    name: 'units',
  })
  export class UnitEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @OneToOne(() => HubEntity)
    @JoinColumn({name: "hub_id"})
    hub: HubEntity;

FYI I also tried coding it like this:

    @OneToOne(type => HubEntity, hub => hub.unit)
    @JoinColumn({name: "hub_id"})
    hub: HubEntity;

For the HUB:

 @Entity({
    name: 'hubs',
  })
  export class HubEntity {
    @PrimaryGeneratedColumn()
    id: number;
  
    @OneToOne(type => UnitEntity, unit => unit.hub)
    @JoinColumn({name: "hub_id"})
    unit: UnitEntity;

For testing, this is how I consoled it.

async findAllByUnit(unitId: number) {
    const unit = await this.unitRepository.findOne(unitId)

    console.log(unit.hub)
  }

It returns undefined

Please let me know what I'm missing.

Thank you in advance~

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have to include the relationship like this:

await this.unitRepository.findOne(unitId, {
    relations: ['hub']
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda