drp,感谢您分享您的模型。我的帖子被删除了,因为我刚刚开始并且需要询问更多看起来很奇怪的信息。无论如何,尝试更改此行:
this.infos = this._createHasOneRepositoryFactoryFor(
'info',
getInfoRepository
);
至
this.infos = this._createHasOneRepositoryFactoryFor(
'infos',
getInfoRepository,
);
框架找不到模型上的“信息”关系,因为您调用了属性“信息”
这是我目前适用于我的示例(运行最新的 lb4 和 postgres):
用户模型.ts
import { model, property, hasOne, Entity } from '@loopback/repository';
import { Address } from './address.model';
@model()
export class User extends Entity {
constructor(data?: Partial<User>) {
super(data);
}
@property({ id: true })
id: number;
@property()
email: string;
@property()
isMember: boolean;
@hasOne(() => Address, {})
address?: Address;
}
地址.model.ts:
import { model, property, belongsTo, Entity } from '@loopback/repository';
import { User } from '../models/user.model';
@model()
export class Address extends Entity {
constructor(data?: Partial<Address>) {
super(data);
}
@property({ id: true })
id: number;
@property()
street1: string;
@property()
street2: string;
@property()
city: string;
@property()
state: string;
@property()
zip: string;
@belongsTo(() => User)
userId: number;
}
用户.repository.ts:
import { HasOneRepositoryFactory, DefaultCrudRepository, juggler, repository } from '@loopback/repository';
import { User, Address } from '../models';
import { PostgresDataSource } from '../datasources';
import { inject, Getter } from '@loopback/core';
import { AddressRepository } from '../repositories'
export class UserRepository extends DefaultCrudRepository<
User,
typeof User.prototype.id
> {
public readonly address: HasOneRepositoryFactory<Address, typeof User.prototype.id>;
constructor(
@inject('datasources.postgres')
dataSource: PostgresDataSource,
@repository.getter('AddressRepository')
protected getAccountRepository: Getter<AddressRepository>,
) {
super(User, dataSource);
this.address = this._createHasOneRepositoryFactoryFor('address', getAccountRepository);
} // end ctor
}
User.controller.ts(长度有所删减):
@get('/users/{id}/address')
async getAddress(
@param.path.number('id') userId: typeof User.prototype.id,
@param.query.object('filter', getFilterSchemaFor(Address)) filter?: Filter,
): Promise<Address> {
return await this.userRepository
.address(userId).get(filter);
}
希望这可以帮助。
祝你好运!