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

225
Visualizações
TypeORM @BeforeInsert not inserting value into Entity field

i am trying to update or insert into a token field before the record is saved but when i use the @BeforeInsert hook, i get this error "error": "Cannot read property 'concat' of undefined" i clearly understand the error, but i dont get why it is happening because this.tokens is a property of the entity user class.

@Entity('users')
class User extends BaseEntity {
@Column({
    type: 'jsonb',
    array: false,
    default: () => "'[]'",
    nullable: false,
  })
  tokens!: Array<{ token: string }>;
}

@BeforeInsert()
  toJSON(): object {
    const user = this;
    user.tokens = user.tokens.concat({ token: "34e5trfkljhkhgufy42343567" });
    // user.save();
    user.resetlink = "https://www.google.com";
    console.log('second', user)
    return user;
  };

and when i use the afterinsert hook it works but the token get assigned twice, which is not what i want.

@AfterInsert()
  toJSON(): object {
    const user = this;
    user.tokens = user.tokens.concat({ token: "34e5trfkljhkhgufy42343567" });
    // user.save();
    user.resetlink = "https://www.google.com";
    console.log('second', user)
    return user;
  };
about 4 years ago · Santiago Trujillo
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