422 when creating multiple products with the same title - BUG

Shopify Partner
16 0 5

Hey @Shopify, I think I found a bug.


When I try to create multiple products with the same title asynchronously only one product gets created and the rest throw a 422 Unprocessable Entity.

 

const shopify = new Shopify(....); // official node api -> shopify-api-node
let products = [
    { title: 'Some name', vendor: 'A' },
    { title: 'Some name', vendor: 'B' },
    { title: 'Some name', vendor: 'C' },
    { title: 'Some name', vendor: 'D' }
]

for(let product of products){
    shopify.product.create(product).then().catch( err => console.error(err) )
}

  
I think the problem lies with the handle creation, because if I run the same code except with handles defined like this:

let products = [
    { title: 'Some name', handle: 'some-name', vendor: 'A' },
    { title: 'Some name', handle: 'some-name-1', vendor: 'B' },
    { title: 'Some name', handle: 'some-name-2', vendor: 'C' },
    { title: 'Some name', handle: 'some-name-3', vendor: 'D' }
]

then it works successfully. 

0 Likes
Community Manager
Community Manager
499 23 54

Hi @Patrick_Hughes,

 

You're correct, it's the handle creation that is causing this. The error I get when attempting this is:

 

{
  "errors": {
    "handle": [
      "has already been taken"
    ]
  }
}

When no handle is specified, we set the handle as the parameterized title. When two same-titled products are created at the same time, we try to create them with the same handle. One will fail in this case. Otherwise, if there was time between the products being created, we'd append a numerical suffix to the handle if a product with the handle already exists.

 

I'm curious what your use case is that this acts as a blocker? I can create an issue internally, but I'm not sure it will be a priority.

1 Like
Highlighted
Shopify Partner
16 0 5

Hey @Busfox 

Thanks for looking into this and explaining what's going on behind the scenes here. It wasn't a blocker for me but it was unexpected. I got around it by placing all existing handles (which I have in the DB) into a lookup, generating a handle on my side, and then running a while loop until the numerical suffix makes the handle unique in the lookup. Another, easier but slower option is to create the products synchronously. The use case is for an epos integration where it's possible to have two products with the same title created at the same time.

0 Likes