我正在基于 Wordpress 数据库架构使用 express 和 typeORM 为 Mysql 构建 rest API,您可以从 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.
最佳答案
我不熟悉该库,但查看它的代码可能会在连接函数中的 this.setParameters(subQueryBuilder.getParameters());
行抛出。要修复,我会在 Chrome 开发工具中启用异常停止(源选项卡最右边的小暂停图标)并在它抛出时查看,我认为这是因为 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;
}
你可以尝试在 repo ( GitHub issue ) 上询问,我认为你只是使用了错误的库。
抱歉,这不是正确的答案,但评论的文字太多。
关于javascript - TypeORM:无法读取未定义的属性 'getParameters',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57840579/