这是我的配置文件。
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”有人知道如何解决这个问题吗?谢谢!
4条答案
按热度按时间xxb16uws1#
您可以使用
where
密钥,例如:7cjasjjr2#
因为你使用的是0.3.x类型,所以你可以用
findOneBy
代替findOne
。nwo49xxi3#
我刚刚把我的typeorm版本从0.3.x降级到0.2.41,它工作了!
gcuhipw94#
要在typeorm〉v0.3.13中使用@nestjs/typeorm修复类型脚本问题,可以在findOne()函数上显式定义where语句的类型。