0

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

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

我将加入两个表,即人员表和信息表。

 - 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 property 'target' of undefined

有没有关于加入的想法?

4

1 回答 1

1

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

希望这可以帮助。

祝你好运!

于 2019-01-08T20:51:00.640 回答