Currently, I am facing an issue where I am attempting to update or insert into a token field before the record is saved. However, when utilizing the @BeforeInsert hook, I encounter the following error:
"error": "Cannot read property 'concat' of undefined"
. I grasp the error message, but it is perplexing why it is occurring since 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;
};
On the other hand, when I utilize the afterinsert hook, it functions as intended but the token ends up being assigned twice, which is not the desired outcome.
@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;
};