MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
945 stars 278 forks source link

[Solved] Getting 422 (Unprocessable Entity) on trying to create or update customer metafield #516

Closed ADTC closed 2 years ago

ADTC commented 2 years ago

I'm attempting to create or update customer metafields. Please let me know what I could be doing wrong:

const Shopify = require('shopify-api-node');

const shopify = new Shopify({
  shopName: myShopifyDomain,
  apiKey: api_key,
  password: password
});

await shopify.customer.update(customer_id, {
  id: customer_id,
  metafields: [{
    key: 'my_key_1',
    value: 'my_value_1',
    value_type: 'string',
    namespace: 'my_ns_1'
  }, {
    key: 'my_key_2',
    value: 'my_value_2',
    value_type: 'string',
    namespace: 'my_ns_2'
  }]
});

I receive HTTPError: Response code 422 (Unprocessable Entity). BTW, if it's important, I'm using this module in AWS Lambda.

ADTC commented 2 years ago

Per your hint on #487 I logged err.response.body and received this in the logs:

{
  errors: {
    'metafields.key': [
      'must be unique within this namespace on this resource',
      'must be unique within this namespace on this resource'
    ]
  }
}

It's because I'm trying to create an existing metafield. In order to update an existing metafield, I need to supply the ID of the metafield as well. When the metafield doesn't exist yet, the id will be an empty string to ensure it is created.

...
  metafields: [{
    id: existing_mf_1 ? existing_mf_1.id : '',
    key: 'my_key_1',
    value: 'my_value_1',
    value_type: 'string',
    namespace: 'my_ns_1'
  }, {
    id: existing_mf_2 ? existing_mf_2.id : '',
    key: 'my_key_2',
    value: 'my_value_2',
    value_type: 'string',
    namespace: 'my_ns_2'
  }]
...

@lpinca By the way, I highly recommend building an expanded documentation for this API with complete examples. It's best done and published in GitHub pages so that you can receive pull requests for documentation updates as well. (I saw that you have a gh-pages branch but it hasn't been updated for over 9 years.)

PS: Feel free to close, if there's nothing else to add.

lpinca commented 2 years ago

Thank you the suggestion but I think it is better to rely on official examples from Shopify API documentation and Shopify Community. This library is only a user friendly wrapper.

Feel free to open a PR to document how to read error details (when available).

ADTC commented 2 years ago

@lpinca true, but I was referring more about having a complete set of examples on how to use your project (in code) in as many scenarios as possible, so that someone new can get started quicker than I did.

I forked your project and ran jsdoc on it. Published here (with the config file and docs folder in my jsdoc branch): https://adtc.github.io/Shopify-api-node/

Of course, this is only autogenerated from all the jsdoc comments you left in the code (thankfully), but it might be a good starting point to improve those comments and even tap into the ability of jsdoc to include examples automatically.

Thank you for your reply btw.