Sync actions fail with error on an undefined field
Description
I was wondering what is expected behaviour from commercetools nodejs sdk perspective in the following scenario:
- Create a customer:
{
"key": "abc",
"email": "[email protected]",
"password": "password"
}
- 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.
@ashishhk yes that is still applicable I was wondering which route would you like to take:
- skipping a property
- setting to an
emptyarray
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