Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

0

You have to include the relationship like this:

await this.unitRepository.findOne(unitId, {
    relations: ['hub']
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda