How to use the overrideCreate function correctly

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”.

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

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

  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)

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: -

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

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

Hello @Enki
Yes it’s fixed thanks !

2 Likes