【发布时间】:2020-12-07 19:11:58
【问题描述】:
我正在使用 typescript 和 typeorm。我有这个实体:
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Sample {
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 50 })
name: string;
@Column('text', { nullable: true })
description: string;
}
我这样查询一个结果:
const connection = await this.getConnection();
const sampleRepo = await connection.getRepository(Sample);
const sample = await sampleRepo.createQueryBuilder('sample')
.where('sample.id = :id', { id: id })
.getOne();
现在,我需要对结果列进行一些处理,但示例对象的类型为 EntitySchema。所以,在打字稿中,我不能做sample.id,因为错误:
Property 'id' does not exist on type 'EntitySchema<any>'
是否可以将EntitySchema 转换为实际的Sample 对象?
【问题讨论】:
-
我猜您的 sampleRepo 创建不正确。它应该类似于
getRepository(Sample) -
我添加了我创建存储库的方式
-
嗯,应该可以。这是文档中的内容
-
您可能想从尝试开始: const sample = await getRepository(Sample).createQueryBuilder('sample') .where('sample.id = :id', { id: id }) 。 getOne();` 链接这些承诺可能会产生不利影响并破坏某处的引用。
标签: node.js typescript typeorm