Reputation: 1151
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;
}
(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.
Upvotes: 4
Views: 10771
Reputation: 51
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
Upvotes: 0
Reputation: 1151
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.
Upvotes: 3
Reputation: 66498
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 comment.
Upvotes: 3