Shopify-Buy-SDK 获取原始产品数据

Shopify-Buy-SDK Get Raw Product Data

我希望使用 ES6、react、react-redux 和您的 js-buy-sdk 构建一个电子商务 Web 应用程序 api。我的问题是,在从 shopify 获取所有产品并将它们放入商店后,我似乎没有像我期望的那样拥有关于产品的所有数据,而只有:{ attrs, shopClient, serializer, type, config, _memoized }.

以下是我调用获取产品的方式,在我命名为 shopify.js:

的文件下
import ShopifyBuy from 'shopify-buy';

const shopClient = ShopifyBuy.buildClient({
  apiKey: 'xxxxxxxxxxxxxxxxxxxx1403c107adc7e',
  domain: 'xxxxxxxxxxxxxxxx.myshopify.com',
  appId: '6',
});

export function fetchAllProducts() {
  return new Promise((resolve, reject) => {
    shopClient.fetchAllProducts()
      .then((data) => {
        console.log('shopClient.fetchAllProducts', data);
        resolve(data);
      }).catch((error) => {
        console.error(new Error('Fetching products error!'));
        reject(error);
      });
  });
}

查看 console.log shopClient.fetchAllProducts 这是我得到的:

我将 data 放在 redux 存储中,当我查看它时,它看起来像这样:

如果相关,请检查 this gist to see the actions I am dispatching and this gist 我的操作在何处减少并放置在商店中。

根据我的理解,当你执行 fetchAllProducts() 方法时,你会得到一个 ProductModel 的数组,因此,我想在我的商店中设置了以下 { attrs, shopClient, serializer, type, config, _memoized } 因为那是ProductModel 内部存储了什么。

我的问题是,有没有办法从 ProductModel 获取原始数据对象(第一个屏幕截图中显示的所有相关产品值和使用来自模型的数据,例如 ProductOptionModelProductModel 依赖),所以我可以出于稳定性和理智的原因放置我的商店?

Shopify API 可以找到参考 here.

在您的示例代码中,您使用 API 密钥调用 API?您确实了解这会使您的商店遭受您可能不希望的滥用,对吗?你确定你知道你在那里做什么吗?

如果您正在寻找对产品返回数据的原始访问,attrs 属性 包含 API 响应 JSON,无需额外的逻辑或楷模。

但是,如果可能的话,我建议您以可以利用这些模型的方式组织您的代码 - 我们添加它们是因为原始数据的格式可能特别混乱(特别是变体、选项、图像) .

就是说,如果您想要所有退回产品的原始数据,像这样的方法应该可以解决问题。

import ShopifyBuy from 'shopify-buy';

const shopClient = ShopifyBuy.buildClient({
  apiKey: 'xxxxxxxxxxxxxxxxxxxx1403c107adc7e',
  domain: 'xxxxxxxxxxxxxxxx.myshopify.com',
  appId: '6',
});

export function fetchAllProducts() {
  return new Promise((resolve, reject) => {
    shopClient.fetchAllProducts()
      .then((products) => {
        resolve(products.map((product) => {
          return product.attrs;
        }));
      }).catch((error) => {
        console.error(new Error('Fetching products error!'));
        reject(error);
      });
  });
}