I have two models related to the hasMany relation.
Customer hasMany CustomerPhones
When creating a new Customer I would like to pass the associated CustomerPhones as part of a single request. This seems like a general need, if the approach I was looking to implement incorrectly, what is the preferred way to do this?
This is the URL to create the client: POST /api/Customers
The request for the above url will be req.body
{ "name": "Foo", "customerPhones": [ { "phoneNumber": "8085551234" }, { "phoneNumber": "8085554567" } ] }
Loopback Model Configurations:
Customer.json
{ "name": "Customer", "base": "User", "properties": { "name": { "type": "string", "required": true } }, "relations": { "customerPhones": { "type": "hasMany", "model": "CustomerPhone", "foreignKey": "" } } }
CustomerPhone.json
{ "name": "CustomerPhone", "base": "PersistedModel", "properties": { "phoneNumber": { "type": "string", "required": true }, "customerId": { "type": "number", "required": true } }, "relations": { "customer": { "type": "belongsTo", "model": "Customer", "foreignKey": "customerId" } } }