Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!