环回4:发送相关数据

问题描述 投票:0回答:1

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

{
  "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时,loopback会抛出以下错误:

{
  "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"
        ]
      }
    }
  }
}

我的猜测是“地址”关系不被视为模型的属性。我怎么解决这个问题?我知道我可以选择单独请求保存地址,但我想避免这种情况。

typescript loopbackjs v4l2loopback
1个回答
1
投票

这个错误实际上是由datasource juggler引发的,它是用于连接数据库的底层连接器库。似乎地址列未在User表中定义您的数据库。请检查一下。

© www.soinside.com 2019 - 2024. All rights reserved.