【问题标题】:shopify-api-node parsing with an extra key使用额外密钥进行 shopify-api-node 解析
【发布时间】:2017-10-02 10:55:34
【问题描述】:

我正在使用 shopify-api-node 在 Shopify 上创建产品。 我使用了这个语法

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)
);

并将其转换为以下代码:

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 是对象 parsedItem 用 Shopify 键值映射 obj

parsedItem 控制台输出

{
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"}
}

作为 index.js 中第 115 行代码的结果 ... 这是使用这个条件

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

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

它将我的 json obj 解析为

{"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"}
}
}

由于 obj 中有额外的“parsedItem”键,它导致发布请求出错。 你能帮我解决这个错误吗?

我是初学者...在此处创建问题之前,我已经将其发布在 StackOverflow 上,没有人回答它。如果有人帮助我,我会很友好。

【问题讨论】:

    标签: javascript json node.js shopify


    【解决方案1】:

    我傻了!!

    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)
                                );
                            });
    

    只需要移除 parsedItem

    周围的 {}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 2012-05-07
      • 2012-07-13
      • 2018-03-04
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多