mysql Typeorm typescript repository finddone-类型的参数不可分配给类型“FindOneOptions”的参数< GuildConfiguration>

cs7cruho  于 2023-03-28  发布在  Mysql
关注(0)|答案(4)|浏览(196)

这是我的配置文件。

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;
}

我试图添加搜索guildId与discordguild.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});它显示
类型为“{ guildId:“string; }”不能分配给“FindOneOptions”类型的参数。对象文本只能指定已知属性,并且“guildId”在类型“FindOneOptions”中不存在。ts(2345)
我的typeorm版本是“typeorm”:“^0.3.1”有人知道如何解决这个问题吗?谢谢!

xxb16uws

xxb16uws1#

您可以使用where密钥,例如:

this.guildConfigRepository.findOne({ 
  where: { 
    guildId: guild.id 
  } 
});
7cjasjjr

7cjasjjr2#

因为你使用的是0.3.x类型,所以你可以用findOneBy代替findOne

const config = await this.guildConfigRepository.findOneBy({guildId:guild.id});
nwo49xxi

nwo49xxi3#

我刚刚把我的typeorm版本从0.3.x降级到0.2.41,它工作了!

gcuhipw9

gcuhipw94#

要在typeorm〉v0.3.13中使用@nestjs/typeorm修复类型脚本问题,可以在findOne()函数上显式定义where语句的类型。

const findOptions: FindOneOptions<T> = {
          where: {
            id: entity.id,
          } as FindOptionsWhere<T>,
        };
return this.repo.findOne(findOptions);

相关问题