Vincit/Objection.js returns "cannot read property '$relation' of undefined" 使用 .withGraphFetched() 时
Vincit/Objection.js returns "cannot read property '$relation' of undefined" when using .withGraphFetched()
我目前正在 Objection.js 使用 NestJS 进行设置,但在使用任何类型的图形提取或保存方法时遇到问题。相关机型:
资源-group.model.ts
import { BaseModel } from './base.model';
import { Model } from 'objection';
import { ResourceGroupTypeModel } from './resource-group-type.model';
import { CollectionModel } from './collection.model';
import { AttributeModel } from './attribute.model';
import { ResourceModel } from './resource.model';
export class ResourceGroupModel extends BaseModel {
static tableName = 'resource_group';
typeId: number;
title: string;
groupId: string;
collectionId?: number;
defaultResourceId?: number;
isDeleted: boolean;
static relationMappings = {
attributes: {
modelClass: AttributeModel,
relation: Model.ManyToManyRelation,
join: {
from: 'resource_group.id',
through: {
from: 'resourcegroup_attribute.resource_group_id',
to: 'resourcegroup_attribute.attribute_id'
},
to: 'attribute.id',
},
},
}
}
attribute.model.ts
import { BaseModel } from './base.model';
import { Model } from 'objection';
export class AttributeModel extends BaseModel {
static tableName = 'attribute'
name: string;
value: string;
isSystem: boolean;
static relationMappings = {
resourceGroups: {
modelClass: `${__dirname}/resource-group.model.js`,
relation: Model.ManyToManyRelation,
join: {
from: 'attribute.id',
through: {
from: 'resourcegroup_attribute.attribute_id',
to: 'resourcegroup_attribute.resource_group_id',
},
to: 'resource_group.id',
},
},
}
}
资源-group.service.ts
import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { EntityService } from '../abstract';
import { ResourceGroup } from '../entities';
import { Repository } from 'typeorm';
import { ModelClass } from 'objection';
import { ResourceGroupModel } from '../db/models/resource-group.model';
@Injectable()
export class ResourceGroupService extends EntityService<ResourceGroup> {
constructor(
@InjectRepository(ResourceGroup)
protected readonly repository: Repository<ResourceGroup>,
@Inject(ResourceGroupModel)
protected readonly model: ModelClass<ResourceGroupModel>,
) {
super(repository);
}
test() {
return this.model
.query()
// .findById(25); // this line works fine
.withGraphFetched(['attributes']) // these two lines do not
.where({id: 25});
}
}
有人知道发生了什么事吗?我查看了源代码,但它似乎是我的代码中的一个问题,可能是我的一个模型中的问题。
所以我在这上面卡了一会儿,原来是一件超级简单的事情。在 resource-group.service.ts
中,我们需要稍作改动。
return this.model
.query()
.withGraphFetched(['attributes'])
.where({id: 25});
需要成为
return this.model
.query()
.withGraphFetched('[attributes]') // square brackets are inside the string itself
.where({id: 25});
代码盲的经典案例,但在 Google 上找不到我的错误消息。希望这对以后真正需要一杯咖啡的人有所帮助(:
我目前正在 Objection.js 使用 NestJS 进行设置,但在使用任何类型的图形提取或保存方法时遇到问题。相关机型:
资源-group.model.ts
import { BaseModel } from './base.model';
import { Model } from 'objection';
import { ResourceGroupTypeModel } from './resource-group-type.model';
import { CollectionModel } from './collection.model';
import { AttributeModel } from './attribute.model';
import { ResourceModel } from './resource.model';
export class ResourceGroupModel extends BaseModel {
static tableName = 'resource_group';
typeId: number;
title: string;
groupId: string;
collectionId?: number;
defaultResourceId?: number;
isDeleted: boolean;
static relationMappings = {
attributes: {
modelClass: AttributeModel,
relation: Model.ManyToManyRelation,
join: {
from: 'resource_group.id',
through: {
from: 'resourcegroup_attribute.resource_group_id',
to: 'resourcegroup_attribute.attribute_id'
},
to: 'attribute.id',
},
},
}
}
attribute.model.ts
import { BaseModel } from './base.model';
import { Model } from 'objection';
export class AttributeModel extends BaseModel {
static tableName = 'attribute'
name: string;
value: string;
isSystem: boolean;
static relationMappings = {
resourceGroups: {
modelClass: `${__dirname}/resource-group.model.js`,
relation: Model.ManyToManyRelation,
join: {
from: 'attribute.id',
through: {
from: 'resourcegroup_attribute.attribute_id',
to: 'resourcegroup_attribute.resource_group_id',
},
to: 'resource_group.id',
},
},
}
}
资源-group.service.ts
import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { EntityService } from '../abstract';
import { ResourceGroup } from '../entities';
import { Repository } from 'typeorm';
import { ModelClass } from 'objection';
import { ResourceGroupModel } from '../db/models/resource-group.model';
@Injectable()
export class ResourceGroupService extends EntityService<ResourceGroup> {
constructor(
@InjectRepository(ResourceGroup)
protected readonly repository: Repository<ResourceGroup>,
@Inject(ResourceGroupModel)
protected readonly model: ModelClass<ResourceGroupModel>,
) {
super(repository);
}
test() {
return this.model
.query()
// .findById(25); // this line works fine
.withGraphFetched(['attributes']) // these two lines do not
.where({id: 25});
}
}
有人知道发生了什么事吗?我查看了源代码,但它似乎是我的代码中的一个问题,可能是我的一个模型中的问题。
所以我在这上面卡了一会儿,原来是一件超级简单的事情。在 resource-group.service.ts
中,我们需要稍作改动。
return this.model
.query()
.withGraphFetched(['attributes'])
.where({id: 25});
需要成为
return this.model
.query()
.withGraphFetched('[attributes]') // square brackets are inside the string itself
.where({id: 25});
代码盲的经典案例,但在 Google 上找不到我的错误消息。希望这对以后真正需要一杯咖啡的人有所帮助(: