环回4:发送相关数据

时间:2019-03-28 01:20:02

标签: typescript loopbackjs v4l2loopback

我有一个表格可以注册一个或多个地址的用户。为简化起见,我将发送回送:

{
  "id": 0,
  "name": "User Name",
  "addresses": [
    {
      "street": "xx",
      "city": "xx",
      "country": "xx"
    },
    {
      "street": "yy",
      "city": "yy",
      "country": "yy"
    }
  ]
}

在我定义的用户模型上(我也有一个地址模型):

export class User extends Entity {
  @property({
    type: 'number',
    id: true,
    required: true,
  })
  id: number;

@property({
    type: 'string',
    required: true,
  })
  name: string;

 @hasMany(() => Address, {keyTo: ‘user_id’})
  Addresses: Array<Address>;
}

我还在UserRepository中定义了:

this.addresses = this.createHasManyRepositoryFactoryFor(
      'addresses',
      AddressRepositoryGetter,
    );

当我发布json时,环回引发以下错误:

{
  "error": {
    "statusCode": 422,
    "name": "ValidationError",
    "message": "The `user` instance is not valid. Details: `addresses` is not defined in the model (value: undefined).",
    "details": {
      "context": "user",
      "codes": {
        "addresses": [
          "unknown-property"
        ]
      },
      "messages": {
        "addresses": [
          "is not defined in the model"
        ]
      }
    }
  }
}

我的猜测是,“地址”关系没有被视为模型的属性。我该如何解决?我知道我可以选择单独请求保存地址,但是我想避免这种情况。

1 个答案:

答案 0 :(得分:1)

此错误实际上是由数据源魔术师抛出的,数据魔术师是用于与数据库连接的基础连接器库。似乎“地址”列未在“用户”表中定义您的数据库。请检查一下。