【问题标题】:TypeORM: Cannot read property 'getParameters' of undefinedTypeORM:无法读取未定义的属性“getParameters”
【发布时间】:2021-10-21 17:31:45
【问题描述】:

我正在使用 Express 和 typeORM 为基于 Wordpress 数据库模式的 Mysql 构建 rest API,您可以从 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()

我的实体:

发布实体:

@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[];

}

发布元实体:

@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.

【问题讨论】:

  • 有错误的堆栈跟踪指向哪里?
  • 感谢您的回复我已经用整个错误日志更新了问题
  • 什么是postmeta?在您的模型中,您有 post
  • 基于 Wordpress,它是一组额外的帖子数据,因为(自定义字段)具有键和值我的帖子元数据和基于 Wordpress 数据库架构的帖子本身之间存在一对一的关系,您可以检查从这里codex.wordpress.org/images/thumb/2/25/WP4.4.2-ERD.png/…
  • 我对 API 不熟悉,但您不需要编写类中数据库表中的每个字段吗?

标签: javascript mysql typescript relational-database typeorm


【解决方案1】:

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

这是源代码中的代码:

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) 上询问,我认为您只是使用库错误。

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

【讨论】:

【解决方案2】:

我得到了代码,但我真的不知道为什么?!! :"D

刚刚删除了我的帖子元实体并重新创建了它,它正在工作!

但我对左加入表格的代码做了一点小改动

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 的一个问题中得到的,但没有在文档中提及。

【讨论】:

    【解决方案3】:

    在我的例子中,我在创建连接对象时没有添加实体属性

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

    Please read the following Stack Overflow answer

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2021-02-27
    • 2021-05-29
    • 1970-01-01
    • 2020-04-04
    • 2019-07-27
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多