Search code examples
mysqltypeorm

Typeorm mysql cannot find by enum value


this is my code

async findAdmin() {
    const user = await this.userRepository.findOne({ role: 0 });
    const user2 = await this.userRepository.findOne({ where: { role: 0 } });
    const users = await this.userRepository.find();
    console.log({ user, user2, users });
}

and this is my log output

{
  user: undefined,
  user2: undefined,
  users: [
    User {
      id: 'a69c0fea-ff08-4fef-949f-3d2717571795',
      phoneNumber: '+989111111111',
      role: 0
    }
  ]
}

What is the problem here? why user is undefined?


Solution

  • As it turned out, you can find numeric enums with stringified value in mysql.

    when you want to find role: 1 it gets the first enum value (as index, not value)

    so for no confusion I decided to use string enums.