# How to use the overrideCreate function correctly

**URL:** <https://community.forestadmin.com/t/how-to-use-the-overridecreate-function-correctly/7311>\
**Category:** Help me!\
**Tags:** howtos\
**Created:** [July 7, 2024, 7:55am UTC](https://community.forestadmin.com/t/how-to-use-the-overridecreate-function-correctly/7311 "2024-07-07T07:55:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quentin\_Somerville1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/quentin_somerville1/32/7114_2.png) [@Quentin\_Somerville1](https://community.forestadmin.com/u/Quentin_Somerville1)\
**Post date:** [July 7, 2024, 7:55am UTC](https://community.forestadmin.com/t/how-to-use-the-overridecreate-function-correctly/7311/1 "2024-07-07T07:55:47Z")

</div>

## Feature(s) impacted

override the create action of a collection.  
I want to override the create action to call my backend.  
Everything works except I get an error message when I call “create”.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/8244499a1b231ac66f88f3d9951ecccb42664661.png)

But the data is well created and I put console.log in the code and the overrideCreate functions returns (maybe the format is wrong but I think I followed the example in the documentation)

So basically I return an array with a single element that is created. Here is my code

```javascript
const overrideCreate: CreateOverrideHandler<
  Schema,
  'my_collection'
> = async context => {
  const { data } = context;

  if (data.length !== 1) {
    throw new UnprocessableError('Cannot create several objects at a time');
  }

  const objectToCreate = data[0];

  const createdObject = await post('/my-collection', {
    email: objectToCreate.email,
    type: objectToCreate.type,
    ...
  }); // This works 

  console.log(JSON.stringify(createdObject)); // here I see my object in the logs

  return [
    {
      id: createdObject.id,
      createdAt: createdObject.createdAt,
      type: createdObject.type,
      name: createdObject.name,
      email: createdObject.user.email,
    },
  ];
};

```

The only thing a bit different as usual is that in this collection I have imported fields from the `users` Collection (which is a oneToMany). I don’t think it’s the issue but we never know

```javascript
  collection.importField('email', { path: 'user:email', readonly: true });

```

After all this I get the error `"Cannot read properties of undefined (reading 'type')"` comming from an external package (not from my code)

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/7/7855ff8979a6099895816bab8b198158d3073296.png)

## Observed behavior

The data is created but I get an error message

## Expected behavior

I don’t get an error message and I’m redirected on the created data page

## Context

- Project name: Azybia
- Team name: Azybia
- Environment name: Production
- Agent technology: nodejs
- Agent “@forestadmin/forest-cloud”: “1.9.17” “@forestadmin/agent”: “1.41.1”,
- Database type: postgres
- Recent changes made on your end if any: -

---

<div class="post-metadata">

**Author:** ![Enki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/enki/32/6004_2.png) [@Enki](https://community.forestadmin.com/u/Enki)\
**Post date:** [July 8, 2024, 12:52pm UTC](https://community.forestadmin.com/t/how-to-use-the-overridecreate-function-correctly/7311/2 "2024-07-08T12:52:55Z")

</div>

Hi @Quentin_Somerville1 ,

The error seems to indicate that the returned data contains a field that is not in your schema. It may be related to the imported field.  
I cannot reproduce the issue, how do you integrate your `overrideCreate` function in your agent ?

kind regards

---

<div class="post-metadata">

**Author:** ![Enki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/enki/32/6004_2.png) [@Enki](https://community.forestadmin.com/u/Enki)\
**Post date:** [July 10, 2024, 1:45pm UTC](https://community.forestadmin.com/t/how-to-use-the-overridecreate-function-correctly/7311/3 "2024-07-10T13:45:57Z")

</div>

Hello @Quentin_Somerville1 ,

We just released a fix that may solve your issue. If some inexistent keys are returned by overrideCreate, they’re now ignored instead of throwing an error.

have a nice day

---

<div class="post-metadata">

**Author:** ![Quentin\_Somerville1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/quentin_somerville1/32/7114_2.png) [@Quentin\_Somerville1](https://community.forestadmin.com/u/Quentin_Somerville1)\
**Post date:** [July 13, 2024, 7:14am UTC](https://community.forestadmin.com/t/how-to-use-the-overridecreate-function-correctly/7311/4 "2024-07-13T07:14:18Z")

</div>

Hello @Enki  
Yes it’s fixed thanks !
