Ad

Shopify-api-node Parsing With An Extra Key

- 1 answer

I am using shopify-api-node to create a product on Shopify. I used this syntax

shopify.metafield.create({
  key: 'warehouse',
  value: 25,
  value_type: 'integer',
  namespace: 'inventory',
  owner_resource: 'product',
  owner_id: 632910392
}).then(
  metafield => console.log(metafield),
  err => console.error(err)
);

and convert it to this code:

self.parsedOutgoingItem(req, item, function(err, parsedItem) {
                            // get the corresponding method name for this type of model request
                            var recordType = modelMapping.key;
                            var postMethod = modelMapping.methods.post;
                            console.log(parsedItem);
                            console.log(JSON.stringify(parsedItem));
                            shopify[recordType][postMethod]({
                                parsedItem
                            }).then(
                                metafields => console.log(metafields),
                                err => console.error(err)
                            );
                        });

item is the object parsedItem is mapped obj with Shopify key values

parsedItem console output

{
images:[ {"src":"https://s3.amazonaws.com/com.rugcopro.photos.turcopersian/images/268d6fd8-cb43-4309-b85e-1e747421df25.png"},                                       {"src":"https://s3.amazonaws.com/com.rugcopro.photos.turcopersian/images/c8af415e-2c6a-44df-9a18-42425389641a.png"}
],
product_type:"Rug",
tags:["Oversize","Kerman Ravar","Rug","traditional",700,"Runner","Rectangle","Blue","Green"],
title:"tirlw hsdka",
variants:{"price":700,"option3":"wool","title":"Rug wool","sku":"13698,27411i","option1":"3.9x17.1","option2":"Green"}
}

as the result of the code in the index.js at line number 115 ... which is using this condition

if (params) {
    const body = key ? { [key]: params } : params;

    options.headers['Content-Type'] = 'application/json';
    options.body = body;
  }

enter image description here

it parsed my json obj as

{"product": `//it is what we need` 
{"parsedItem":  `//it is what comes up which is causing the error`
{
images:[ {"src":"https://s3.amazonaws.com/com.rugcopro.photos.turcopersian/images/268d6fd8-cb43-4309-b85e-1e747421df25.png"}, {"src":"https://s3.amazonaws.com/com.rugcopro.photos.turcopersian/images/c8af415e-2c6a-44df-9a18-42425389641a.png"}
],
product_type:"Rug",
tags:["Oversize","Kerman Ravar","Rug","traditional",700,"Runner","Rectangle","Blue","Green"],
title:"tirlw hsdka",
variants:{"price":700,"option3":"wool","title":"Rug wool","sku":"13698,27411i","option1":"3.9x17.1","option2":"Green"}
}
}

because of that extra "parsedItem" key in the obj, it is causing the post request with an error. can you help me how can I overcome this error, please?

I am a beginner ... before creating an issue here I already posted it on StackOverflow where no one answered it.. it will be kind if someone helps me out.

Ad

Answer

silly me!!

self.parsedOutgoingItem(req, item, function(err, parsedItem) {
                            // get the corresponding method name for this type of model request
                            var recordType = modelMapping.key;
                            var postMethod = modelMapping.methods.post;
                            console.log(parsedItem);
                            console.log(JSON.stringify(parsedItem));
                            shopify[recordType][postMethod](
                                parsedItem
                            ).then(
                                metafields => console.log(metafields),
                                err => console.error(err)
                            );
                        });

just need to remove the {} around parsedItem

Ad
source: stackoverflow.com
Ad