我正在使用可通过here检查的Wordpress数据库模式,将Express与typeORM for Mysql一起使用构建REST API。

当我尝试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.

最佳答案

我不熟悉该库,但查看它的代码可能会在join函数中的this.setParameters(subQueryBuilder.getParameters());行处抛出。要修复,我将在Chrome Dev工具中启用“异常停止”(最右边的“源”选项卡上的小暂停图标),并在抛出异常时查看,我认为这是因为您传递的WpPostMetaModel不符合接口(interface),它应该是一个函数(在您的如果您传递类-在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;
}

您可以尝试在存储库(GitHub issue)上询问,我认为您使用的库错误。

抱歉,这不是确切的答案,但是有太多文本需要评论。

关于javascript - TypeORM:无法读取未定义的属性 'getParameters',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57840579/

10-09 20:07