Typeorm typescript repository findone - Argument of type is not assignable to parameter of type 'FindOneOptions<GuildConfiguration>'
Asked Answered
E

6

9

Here is my configuration file.

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity({ name: 'guild_configurations' })
export class GuildConfiguration {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true, name: 'guild_id' })
  guildId: string;

  @Column({ default: '?' })
  prefix: string;

  @Column({ name: 'welcome_channel_id', nullable: true })
  welcomeChannelId: string;
}

I was trying to add search guildId with discord guild.id

// https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-guildCreate
import { Guild } from 'discord.js';
import BaseEvent from '../utils/structures/BaseEvent';
import DiscordClient from '../client/client';
import { getRepository, ObjectID } from 'typeorm';
import { GuildConfiguration } from '../typeorm/entities/GuildConfiguration';

export default class GuildCreateEvent extends BaseEvent {
  constructor(
    private readonly guildConfigRepository = getRepository(GuildConfiguration)
  ) {
    super('guildCreate');
  }
  
  async run(client: DiscordClient, guild: Guild) {
    console.log("Hello World");
    console.log(`Joined ${guild.name}`);
    const config = await this.guildConfigRepository.findOne({guildId:guild.id});
    if(config){
      console.log("A configuration was found!")
    }else{
      console.log("Configuration was not found. Creating one...")
      const newConfig = this.guildConfigRepository.create({guildId:guild.id})
      return this.guildConfigRepository.save(newConfig);
    }
    
  }
}

this.guildConfigRepository.findOne({guildId:guild.id}); it shows

Argument of type '{ guildId: string; }' is not assignable to parameter of type 'FindOneOptions'. Object literal may only specify known properties, and 'guildId' does not exist in type 'FindOneOptions'.ts(2345)

my typeorm version is "typeorm": "^0.3.1" Does anyone know how to fix this? Thanks!

Elainaelaine answered 23/3, 2022 at 9:58 Comment(3)
What if you do: this.guildConfigRepository.findOne({ where: { guildId: guild.id } });?Silencer
good point! But have downgraded, would try it out next time :)Elainaelaine
@Silencer it works, thanks a lot! Guess, you can write it like the answerDrayton
S
23

You can use the where key instead such as:

this.guildConfigRepository.findOne({ 
  where: { 
    guildId: guild.id 
  } 
});
Silencer answered 8/4, 2022 at 13:46 Comment(2)
It is also known as TS2345 at nestjs.Hydranth
this worked for typeorm version 0.3.10Morlee
I
17

As you're using the 0.3.x typeorm you might use the findOneBy instead of findOne.

const config = await this.guildConfigRepository.findOneBy({guildId:guild.id});
Illiquid answered 9/6, 2022 at 15:7 Comment(2)
Argument of type '{ id: number; }' is not assignable to parameter of type 'FindOptionsWhere<Student> | FindOptionsWhere<Student>[]'.Dallis
I do have the same Issue as you @KokHowTeh, did you find a solution?Orff
A
2

To fix the typescript issue using @nestjs/typeorm with typeorm > v0.3.13, you can explicitly define the type of where statement on findOne() function.

const findOptions: FindOneOptions<T> = {
          where: {
            id: entity.id,
          } as FindOptionsWhere<T>,
        };
return this.repo.findOne(findOptions);
Awe answered 23/3, 2023 at 16:38 Comment(1)
Thanks, this is great and also allows to build query methods with generic classes which resulted in compilation errors otherwise.Unavailing
E
1

I just downgraded my typeorm version from 0.3.x to 0.2.41 and it worked!

Elainaelaine answered 23/3, 2022 at 10:35 Comment(2)
no need to download you can use @almaju's answer (just add where clause)Morlee
@AshiqDey except that "just don't use this version" isn't really a solution.Sob
S
1

I tried the chosen answer's solution, but without success. So I found a solution pointed out in TypeORM GitHub's issue page: https://github.com/typeorm/typeorm/issues/8954#issuecomment-1198545676. It seems that the newer TypeORM's version came with some changes that leads to those errors.

The solution, basically is to add the where's argument into a Equal function, as show as below:

this.usersRepository.findOne({
    where: {
        email: Equal(authUser.email),
        password: Equal(authUser.password),
    }
})

You'll need to import the Equal function from TypeORM too:

import { Repository, Equal } from "typeorm";
Swap answered 5/11, 2023 at 1:31 Comment(0)
C
0

Just use findOneBy method:

this.guildConfigRepository.findOneBy({ 
  guildId: guild.id
});
Crunch answered 23/1 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.