TypeORM:无法读取未定义的 属性 'getParameters'

TypeORM: Cannot read property 'getParameters' of undefined

我正在构建 rest API 使用带有 typeORM 的 express for Mysql 基于 Wordpress 数据库架构,您可以从 here 中检查。

当我尝试 inner joinleft join 时,我每次都会收到 Cannot read property 'getParameters' of undefined 错误。

我的代码:

const posts = await this.postRepository
    .createQueryBuilder('posts')
    .innerJoinAndSelect(WpPostMetaModel, 'postmeta', 'posts.id = postmeta.post')
    .getMany()

response.send(posts);

我也试过以下同样的错误:

let posts = await createQueryBuilder('WpPostsModel')
    .leftJoinAndSelect(WpPostMetaModel, 'postmeta', 'postmeta.post_id = WpPostsModel.ID')
    .getManyAndCount()

我的实体:

Post实体:

@Entity({ name: 'wp_posts', synchronize: false })
export class WpPostsModel {

    @PrimaryGeneratedColumn({ name: 'ID' })
    public id?: number;

    @Column({ name: 'post_date' })
    public date?: Date;

    @Column({ name: 'post_date_gmt' })
    public date_gmt?: Date;

    ...etc

    @ManyToMany(() => WpTermTaxonomyModel)
    @JoinTable({
        name: 'wp_term_relationships',
        joinColumn: {
            name: 'object_id',
            referencedColumnName: 'ID'
        },
        inverseJoinColumn: {
            name: 'term_taxonomy_id',
            referencedColumnName: 'termTaxonomyId'
        }
    })
    public categories?: WpTermTaxonomyModel[];

}

Post 元实体:

@Entity({ name: 'wp_postmeta' })
export class WpPostMetaModel {

    @PrimaryGeneratedColumn({ name: 'meta_id' })
    public metaId?: number;

    @OneToOne(() => WpPostsModel, {eager: true, cascade: true})
    @JoinColumn({ name: 'post_id' })
    public post?: WpPostsModel

    @Column({ name: 'meta_key' })
    public metaKey?: string;

    @Column({ name: 'meta_value' })
    public metaValue?: string;

}

更新:整个错误

(node:1043) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getParameters' of undefined
    at SelectQueryBuilder.join (/Volumes/Partion-2/Projects/republic-news/src/query-builder/SelectQueryBuilder.ts:1319:52)
    at SelectQueryBuilder.leftJoin (/Volumes/Partion-2/Projects/republic-news/src/query-builder/SelectQueryBuilder.ts:284:14)
    at SelectQueryBuilder.leftJoinAndSelect (/Volumes/Partion-2/Projects/republic-news/src/query-builder/SelectQueryBuilder.ts:364:14)
    at WpPostsController.<anonymous> (/Volumes/Partion-2/Projects/republic-news/src/controllers/post.controller.ts:54:14)
    at step (/Volumes/Partion-2/Projects/republic-news/src/controllers/post.controller.ts:33:23)
    at Object.next (/Volumes/Partion-2/Projects/republic-news/src/controllers/post.controller.ts:14:53)
    at /Volumes/Partion-2/Projects/republic-news/src/controllers/post.controller.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/Volumes/Partion-2/Projects/republic-news/src/controllers/post.controller.ts:4:12)
    at WpPostsController.getAllPosts (/Volumes/Partion-2/Projects/republic-news/src/controllers/post.controller.ts:31:86)
(node:1043) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1043) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我不熟悉该库,但查看它的代码可能会在 join 函数的 this.setParameters(subQueryBuilder.getParameters()); 行抛出。要解决这个问题,我会在 Chrome 开发工具(源选项卡最右边的小暂停图标)中启用异常停止并在它抛出时查看,我认为这是因为 WpPostMetaModel 你通过的不符合界面它应该是一个函数(在你的情况下你传递 class - 在 JS 中是相同的) return 正确的数据:

这是源代码中的代码:

let subQuery: string = "";
if (entityOrProperty instanceof Function) {
    const subQueryBuilder: SelectQueryBuilder<any> = (entityOrProperty as any)(((this as any) as SelectQueryBuilder<any>).subQuery());
    this.setParameters(subQueryBuilder.getParameters()); // I think this line throw error
    subQuery = subQueryBuilder.getQuery();

} else {
    subQuery = entityOrProperty;
}

你可以尝试在 repo (GitHub issue) 上询问,我想你只是用错了库。

抱歉,这不是正确的答案,但是评论的文字太多了。

我的代码可以正常工作,但我真的不知道为什么?!! :"D

刚刚删除了我的 post 元实体并重新创建,它运行良好!

但是我对代码做了一些小的改动以左加入 table

const posts = await this.postRepository.createQueryBuilder('post')
    .leftJoinAndMapOne('post.meta', WpPostmetaModel, 'postmeta', 'postmeta.post  = post.id') 
    // leftJoinAndMapOne instead of letftJoinAndSelect as leftJoinAndSelect didn't return the joined table
    .getMany();

response.send(posts);

我从 github 的一个问题中得到它,不幸的是没有在文档中提及。

就我而言,我在创建连接对象时没有添加实体属性

我认为“无法读取未定义的 属性 'getParameters'”意味着它找不到特定 table(实体 class)的元数据。

Please read the following Stack Overflow answer