#Extend Customer Data Model - storefront to backend

10 messages · Page 1 of 1 (latest)

odd pilot
fluid shoal
#

This has been flagged multiple times. I feel like the Medusa team is too busy working and releasing other stuff right now

patent heath
#

Hi @odd pilot,

Here’s what you need to do:

1. Create a module with a custom model and set up a link to the customer model. You can refer to this guide:

Defining and Linking Models

2. The recommended way to create data in the custom model is by using a workflow. You can find more details here:
[Workflows](https://docs.medusajs.com/learn/basics/workflows)
    Step 1: Create a record in the database and return the ID of the custom model.
    Step 2: Link the custom model to the customer.

3. Once the record is linked properly, you can use a query in your custom API route to retrieve the information. This guide will help:

Querying Linked Records

odd pilot
#

Hi, this part works. My problem is trying to pass additional_data from storefront when customer registering.

odd pilot
#

I can create user with additional data on postman: POST http://localhost:9000/admin/customers

{
"email": "[email protected]",
"first_name": "Dorothy",
"last_name": "Clarke",
"phone": "64815967873",
"additional_data": {
"gender": "Man",
"birthday": "1992-07-13",
"nationality": "FR",
"residence": "NL",
"city": "Amsterdam",
"newsletter": true
}
}

customer is created, profile is linked to the customer - everthing works. I can see additional data on database and admin.

#

but not sure how to pass this data on registration form on next storefront

patent heath
#

Ok so if u want, pass data from storefront u need use a /store/customer endpoint with this same logic as /admin/customers

odd pilot
#

I'm getting - error: Invalid request: Unrecognized fields: 'additional_data' when trying that

#

middleware:
// Store API route for customer registration
{
method: "POST",
matcher: "/store/auth",
additionalDataValidator: {
gender: z.string().optional(),
birthday: z.string().optional(),
nationality: z.string().optional(),
residence: z.string().optional(),
city: z.string().optional(),
newsletter: z.boolean().optional(),
},
},

// Store API route for customer creation
{
  method: "POST",
  matcher: "/store/customers",
  additionalDataValidator: {
    gender: z.string().optional(),
    birthday: z.string().optional(),
    nationality: z.string().optional(),
    residence: z.string().optional(),
    city: z.string().optional(),
    newsletter: z.boolean().optional(),
  },
},

// Store API route for customer updates
{
  method: "POST",
  matcher: "/store/customers/me",
  additionalDataValidator: {
    gender: z.string().optional(),
    birthday: z.string().optional(),
    nationality: z.string().optional(),
    residence: z.string().optional(),
    city: z.string().optional(),
    newsletter: z.boolean().optional(),
  },
},