nodejs icon indicating copy to clipboard operation
nodejs copied to clipboard

Sync actions fail with error on an undefined field

Open kamaz opened this issue 5 years ago • 2 comments

Description

I was wondering what is expected behaviour from commercetools nodejs sdk perspective in the following scenario:

  1. Create a customer:
{
  "key": "abc",
  "email": "[email protected]",
  "password": "password"
}
  1. Use sync actions using the same payload:
{
  "key": "abc",
  "email": "[email protected]",
  "password": "password"
}

It fails with an error:

TypeError: Cannot read property '0' of undefined
    at /node_modules/@commercetools/sync-actions/dist/sync-actions.cjs.js:496:73
    at Array.forEach (<anonymous>)
    at buildArrayActions (/node_modules/@commercetools/sync-actions/dist/sync-actions.cjs.js:484:31)
    at actionsMapAddresses (/node_modules/@commercetools/sync-actions/dist/sync-actions.cjs.js:906:10)
    at /node_modules/@commercetools/sync-actions/dist/sync-actions.cjs.js:951:14
    at mapActionGroup (/node_modules/@commercetools/sync-actions/dist/sync-actions.cjs.js:264:51)
    at doMapActions (/node_modules/@commercetools/sync-actions/dist/sync-actions.cjs.js:950:21)
    at Object.buildActions (/node_modules/@commercetools/sync-actions/dist/sync-actions.cjs.js:250:12)
    at /packages/data-import/dist/lib/sync.js:37:33
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

It looks like sync actions fail to merge arrays when the new resource does not have a property defined. In the case of the customer type when a customer is created following properties are defined with empty array addresses, shippingAddressIds, billingAddressIds and stores.

Expected Behavior

Sync actions to skip a property for new resource when old resource has empty array and the property is not defined.

Alternatively, add the property with empty array which enables sync actions to resolve actions accordingly. (probably easier)

Current Behavior

Throws an error.

Possible Solution

I'm happy to create PR if that is expected behaviour.

kamaz avatar May 05 '20 13:05 kamaz

@ashishhk yes that is still applicable I was wondering which route would you like to take:

  1. skipping a property
  2. setting to an empty array

kamaz avatar Jun 14 '20 13:06 kamaz

Thanks @kamaz for reporting this 👍 well, we had same issue for categories and this PR solved it 1505.

Basically, we follow second suggestion that you suggested by using this function copyEmptyArrayProps which changed lately to make it more robust 1532

I believe that we just need to add copyEmptyArrayProps to createBuildActions for customer like here 1505

markus-azer avatar Jun 15 '20 13:06 markus-azer