我怎样才能在 loopback4 中加入两个 table

How can i do the join of two table in loopback4

我在我的代码中创建了下面提到的控制器、模型和存储库。请看

我已经开发了下面提到的代码,但仍然无法执行连接操作。

我要加入两个table,即人员和信息table。

 - Info table having one foreign key which is belong to person table.
 
 - Person table: id, name, status
 
 - Info table : id, person_id , name , status

我还为信息和人员创建了存储库、模型和控制器文件。

人物资料库(person.repository.ts)

) {
  super(Person, dataSource);
  this.infos = this._createHasOneRepositoryFactoryFor(
    'info',
    getInfoRepository,
 );
}

人物模块(person.module.ts)

 @hasOne(() => Info)
    infos?: Info;
 
 constructor(data?: Partial<Person>) {
  super(data);
 }

信息模块(info.module.ts)

 @belongsTo(() => Person)
 personId: number;

 constructor(data?: Partial<Info>) {
    super(data);
 }

它向我显示这样的错误 GET /people/fetchfromtwotable?filter[offset]=0&filter[limit]=10&filter[skip]=0: 500 TypeError: Cannot read 属性 'target' of undefined[ 未处理错误=34=]

有加盟的想法吗?

drp,感谢您分享您的模型。我的 post 被删除了,因为我刚刚开始,需要询问更多看起来很奇怪的信息。无论如何,尝试更改此行:

 this.infos = this._createHasOneRepositoryFactoryFor(
'info',
getInfoRepository
);

 this.infos = this._createHasOneRepositoryFactoryFor(
'infos',
getInfoRepository,
);

框架无法在模型上找到 'info' 关系,因为您调用了 属性 'infos'

这是我目前适用的示例(运行 最新的 lb4 和 postgres):

User.model.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;
}

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;

}

User.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);
  }

希望对您有所帮助。

祝你好运!