最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeORM: Cannot read property 'getParameters' of undefined - Stack Overflow

programmeradmin1浏览0评论

I'm building rest API using express with typeORM for Mysql based on Wordpress database schema you can check from here.

When I try to inner join or left join I get that Cannot read property 'getParameters' of undefined error every time.

My code:

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

response.send(posts);

I tried also the following with the same error:

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

My entities:

Post entity:

@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 meta entity:

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

}

update: the whole error

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

I'm building rest API using express with typeORM for Mysql based on Wordpress database schema you can check from here.

When I try to inner join or left join I get that Cannot read property 'getParameters' of undefined error every time.

My code:

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

response.send(posts);

I tried also the following with the same error:

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

My entities:

Post entity:

@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 meta entity:

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

}

update: the whole error

(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.
Share Improve this question edited Sep 8, 2019 at 9:21 hesham shawky asked Sep 8, 2019 at 9:11 hesham shawkyhesham shawky 1,1516 gold badges22 silver badges48 bronze badges 5
  • 1 Where stack trace with the error is pointing? – jcubic Commented Sep 8, 2019 at 9:17
  • Thanks for the reply I have updated the question with the whole error log – hesham shawky Commented Sep 8, 2019 at 9:23
  • 1 What is postmeta? in your model you have post. – jcubic Commented Sep 8, 2019 at 9:54
  • Based on Wordpress it's an extra set of data for posts as ( custom fields ) have key and value I have one to one relation between my post meta and post itself based on Wordpress database schema as you could check from here codex.wordpress/images/thumb/2/25/WP4.4.2-ERD.png/… – hesham shawky Commented Sep 8, 2019 at 10:02
  • I'm not familar with the API as I said but don't you need to write every single field from database table inside your class? – jcubic Commented Sep 8, 2019 at 10:04
Add a ment  | 

3 Answers 3

Reset to default 3

I'm not familiar with the library but looking at it's code it probably throw at line this.setParameters(subQueryBuilder.getParameters()); in join function. To fix I would enable stop on exception in Chrome Dev tools (source tab little pause icon on far right) and see while it throw, I think it's because WpPostMetaModel you're passing don't meet interface it should be a function (in your case you passing class - in JS is the same) that return proper data:

this is the code in source code:

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;
}

You can try to ask on the repo (GitHub issue), I think you just using the library wrong.

Sorry this is not exactly the answer, but there is too much text for ment.

I got the code work but I really don't know why?!! :"D

Just removed my post meta entity and recreated again and it's boooom working!

But I did little small change to the code to left join the 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);

I got it from a question from github and didn't mention on the docs, unfortunately.

In my case, I did not add entities property while creating the connection object

I think "Cannot read property 'getParameters' of undefined" means it can't find the metadata of the particular table (Entity class).

Please read the following Stack Overflow answer

发布评论

评论列表(0)

  1. 暂无评论