Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

This seems like such a simple question to answer, but finding an answer for this seems impossible.

I am building a password reset feature for a backend application with Express and Typescript. I am using Postgres for the database and Typeorm for data manipulation. I have a User entity with these two columns in my database:

@Column({
    unique: true,
    nullable: true,
resetPasswordToken!: string;
@Column({ nullable: true, type: 'timestamp with time zone' })
resetPasswordExpiresAt!: Date;

When a user requests a password reset token the resetPasswordToken and resetPasswordExpiresAt fields get both filled with the desired values. With the token that was sent to the user's e-mail address, the user can reset his/her password. After the user's password is reset, I want to clear these two fields by setting them to null:

user.resetPasswordToken = null;
user.resetPasswordExpiresAt = null;
user.save()

But if I do this Typescript complains about the two lines where I assign the null value:

Type 'null' is not assignable to type 'string'.

Type 'null' is not assignable to type 'Date'.

If I change the columns in my entity to accept null like below, the errors disappear:

resetPasswordToken!: string | null;
resetPasswordExpiresAt!: Date | null;

But when I start my Express application I get the following error when Typeorm tries to connect to my database:

Data type "Object" in "User.resetPasswordToken" is not supported by "postgres" database.

How do I set these fields to null?

what's the version of ts-node you use? check here: github.com/TypeStrong/ts-node/issues/569 – Mahdi Ghajary Nov 1, 2020 at 19:43 @Mahdi I am using the latest version (9.0.0) of ts-node. The solution in the link you sent me did not work for me. – Errol59 Nov 2, 2020 at 10:25

After a good night rest I managed to solve my problem.

Typeorm sets the type of the database fields based on the typing you give the variables for your entities in typescript. Typeorm casts the code below to a varchar field in my postgres database because I gave it a string as a type in typescript.

@Column({
    unique: true,
    nullable: true,
resetPasswordToken!: string;

This is also where lies my problem. Typeorm takes the typing of a field and tries to create that database field based on the typing it reads. While the code below is correct, typescript basically encapsulates both types in a single object and that object is what is being read by Typeorm causing the error that I got.

resetPasswordToken!: string | null;

To fix my problem I had to specifiy the database field type explicitly like this:

@Column({
    type: 'text',
    unique: true,
    nullable: true,
resetPasswordToken!: string;
                @superJustin, after adding type: 'text' to the column decorator you can add string | null for type checking.
– Errol59
                Feb 25, 2021 at 14:59

The accepted answer is not exactly correct. Default typeorm conversion from string type is "varchar" on MySQL DB. So if you use type: "text" it will define the column incorrectly. If you want to make it compatible with default behavior you should use typescript types like this

@Column({
    type: String,
    unique: true,
    nullable: true,
resetPasswordToken!: string | null;

To fix my problem I had to specifiy the database field type explicitly like this:

name: "tag_id", type: "varchar", isNullable: true

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.