In my registration form, I have the option to add one or more addresses for a user. In order to streamline this process, I am using loopback:
{
"id": 0,
"name": "User Name",
"addresses": [
{
"street": "xx",
"city": "xx",
"country": "xx"
},
{
"street": "yy",
"city": "yy",
"country": "yy"
}
]
}
Within my User model (which also includes an Address model), I have defined:
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>;
}
Additionally, in UserRepository, I have specified:
this.addresses = this.createHasManyRepositoryFactoryFor(
'addresses',
AddressRepositoryGetter,
);
Upon submitting the JSON data, loopback returns the following error message:
{
"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"
]
}
}
}
}
It seems that the "addresses" relation is not recognized as a property of the model. How can I resolve this issue? I am aware that I could make a separate request to save the addresses, but I would prefer to avoid that solution.