meteor-shopify : expected String to be a Hash

3.2k Views Asked by At

I'm using froatsnook:shopify trying to modify a custom collection's metafields.

Server JS

/**
 * Modify Shopify Custom Collection Metafields
 * @request PUT /admin/custom_collections/#{id}.json
 * 
 * @param  {Number}   collection_id 
 * @param  {Object}   collection_data
 * @param  {Function} callback
 */
modifyShopifyCustomCollectionMetafields: function(collection_id, collection_data, callback) {

  var meta = ShopifyAPI.modifyCustomCollection({
    id: collection_id,
    custom_collection : JSON.stringify( collection_data )
  })

  if ( AdminConfig.debug.server ) console.log( 'modifyShopifyCustomCollectionMetafields', meta )

  if ( callback ) callback( meta )

  return meta;

},

Client JS

Meteor.call('modifyShopifyCustomCollectionMetafields', collection_id, {
  'id': collection_id,
  'metafields' : [
  {
    'key' : 'color_primary',
    'value' : design_settings.colors.primary,
    'value_type' : 'string',
    'namespace' : 'store',
  },
  {
    'key' : 'color_dark',
    'value' : design_settings.colors.primary_dark,
    'value_type' : 'string',
    'namespace' : 'store',
  },
  {
    'key' : 'color_light',
    'value' : design_settings.colors.primary_light,
    'value_type' : 'string',
    'namespace' : 'store',
  },
  ]
}, function (data) {
  console.log( 'Clientside callback', data )
})

All looks fine to be, but then I get this in the (server) console:

PUT https://<MY_STORE_NAME>.myshopify.com/admin/custom_collections/42393729.json?custom_collection={"id":"42393729","metafields":[{"key":"color_primary","value":"#5c28a4","value_type":"string","namespace":"store"},{"key":"color_dark","value":"#401a74","value_type":"string","namespace":"store"},{"key":"color_light","value":"#a42da8","value_type":"string","namespace":"store"}]}

Exception while invoking method 'modifyShopifyCustomCollectionMetafields' Error: failed [400] {"errors":{"custom_collection":"expected String to be a Hash"}}

Note that if I remove JSON.stringify(...) from the serverside JS, it will try to send [Object object] in the request URI.

Any ideas?

2

There are 2 best solutions below

0
On BEST ANSWER

See comment on @ilrein question for details.

Appears to be an issue with package itself.

Here is a simple client API (for private apps with basic auth) I made to circumvent the issue: Github gist

2
On

I think there may be a bug in the package. Have you successfully used the API to POST before? I'm not sure if the issue is in scope for all POST requests, or for only some (using the awesome froatsnook package).

I've made an issue where I too, faced a POST request that returned "string expects to be a hash".

I'm temporarily sidestepping this issue by using plain old HTTP.Post and passing in an object with what the Shopify API specifically asks for:

var options = {
  data: params,
  headers: {
    'X-Shopify-Access-Token': Meteor.user().profile.shopifyAccessToken
  }
};

var newScript = HTTP.post("https://" + Meteor.user().profile.shopName + ".myshopify.com/admin/script_tags.json" , options);