我正在使用可与here进行检查的Wordpress数据库架构基于MySQL的typeORM for Mysql构建rest API。
当我尝试inner join
或left 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.
答案 0 :(得分:1)
我不熟悉该库,但查看它的代码可能会在join函数的第this.setParameters(subQueryBuilder.getParameters());
行处抛出。要修复,我将在Chrome Dev工具中启用“异常停止”(最右边的“源”选项卡上的小暂停图标),并在抛出异常时查看,我认为这是因为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;
}
您可以尝试在仓库(GitHub issue)上询问,我认为您使用的库是错误的。
很抱歉,这并不是确切的答案,但是有太多文本需要评论。
答案 1 :(得分:0)
我完成了代码工作,但我真的不知道为什么?! :“ 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上的一个问题中得到的,而文档中没有提及。