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.