我正在使用express构建rest API,基于Wordpress数据库模式,您可以从here查看。
当我尝试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.发布于 2019-09-09 00:59:28
我得到了代码工作,但我真的不知道为什么?!!:"D
刚刚删除了我的帖子元实体,并重新创建,它的boooom工作!
但是我对代码做了很小的改动,以加入表的左侧
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的一个问题中得到了它,不幸的是,在文档中没有提到。
发布于 2019-09-08 17:45:27
我不熟悉这个库,但是看一下它的代码,它可能会在连接函数的this.setParameters(subQueryBuilder.getParameters());行抛出。为了解决这个问题,我会在Chrome Dev工具中启用stop on exception (源码标签页的小暂停图标在最右边),然后看它抛出的时候,我认为这是因为你传递的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)上问一下,我想你只是用错了这个库。
很抱歉,这不是确切的答案,但有太多的文本可供评论。
发布于 2021-10-21 08:34:08
在我的示例中,在创建connection对象时没有添加entities属性
我认为"Cannot read property 'getParameters‘of undefined“意味着它找不到特定表(Entity类)的元数据。
https://stackoverflow.com/questions/57840579
复制相似问题