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

227
Vistas
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 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