我正在尝试使用 loopback4 belongsTo 关系,但我得到相同的错误“TypeError:无法读取未定义的属性'目标'”
源模型
每个房间都应该有一个类型
export class Rooms extends Entity {
@property({
type: 'string',
id: true,
})
_id: string;
@property({
type: 'number',
})
rating?: number;
// relation ###
@belongsTo(() => RoomsTypes)
roomsTypesId: string;
constructor(data?: Partial<Rooms>) {
super(data);
}
}
目标模型
房间类型模型
@model({settings: {strict: false}})
export class RoomsTypes extends Entity {
@property({
type: 'string',
id: true,
})
_id: string;
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'number',
required: true,
})
numOfPeople: number;
@property({
type: 'array',
itemType: 'string',
required: true,
})
features: string[];
constructor(data?: Partial<RoomsTypes>) {
super(data);
}
}
源存储库
我认为错误就在这里
import {
DefaultCrudRepository,
juggler,
repository,
BelongsToAccessor,
} from '@loopback/repository';
import { Rooms, RoomsRelations, RoomsTypes, RoomsTypesRelations} from '../models';
import {inject, Getter} from '@loopback/core';
import { RoomsTypesRepository } from './rooms-types.repository';
export class RoomsRepository extends DefaultCrudRepository<
Rooms,
typeof Rooms.prototype._id,
RoomsRelations
> {
public readonly roomTypes: BelongsToAccessor<
RoomsTypes,
typeof Rooms.prototype._id
>;
constructor(
@inject('datasources.db') protected DbDataSource: juggler.DataSource,
@repository.getter('RoomsTypesRepository')
RoomsTypesRepositoryGetter: Getter<RoomsTypesRepository>,
) {
super(Rooms, DbDataSource);
this.roomTypes = this.createBelongsToAccessorFor(
'roomsTypesId',
RoomsTypesRepositoryGetter,
);
}
}
我得到同样的错误“TypeError:无法读取未定义的属性'目标'”