Eduardo
Eduardo

Reputation: 1831

Loopback 4: Sending related data

I have a form were I can register a user with one or more addresses. To simplify things, I am sending to loopback:

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

On my User model I defined (I have an Address model too):

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

Also in UserRepository I has defined:

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

When I post the json, loopback throws the following error:

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

My guess is that "addresses" relation is not being considered a property of the model. How can I solve this?. I know I have the option to do a separate request to save the addresses, but I want to avoid that.

Upvotes: 0

Views: 60

Answers (1)

Samarpan
Samarpan

Reputation: 943

This error is actually thrown by datasource juggler which is the underlying connector library for connecting with database. It seems addresses column is not defined your database in the User table. Please check that.

Upvotes: 1

Related Questions