刚开始使用Shopify,并试图获得订单。按照Shopify API文档,这里是我的代码:
const Shopify = require('@shopify/shopify-api');
const client = new Shopify.Clients.Rest('my-store.myshopify.com',
process.env.SHOPIFY_KEY);
module.exports.getShopifyOrderById = async (orderId) => {
return await client.get({
path: `orders/${orderId}`,
});
}
当我执行此代码时,我得到以下错误:
TypeError: Cannot read properties of undefined (reading 'Rest')
似乎不知道问题出在哪里。
您需要使用Object destruction来获取Shopify对象,或者使用如下默认导出。
const { Shopify } = require('@shopify/shopify-api');
const client = new Shopify.Clients.Rest('my-store.myshopify.com',
process.env.SHOPIFY_KEY);
或
const Shopify = require('@shopify/shopify-api').default;
const client = new Shopify.Clients.Rest('my-store.myshopify.com',
process.env.SHOPIFY_KEY);
或
const ShopifyLib = require('@shopify/shopify-api');
const client = new ShopifyLib.Shopify.Clients.Rest('my-store.myshopify.com',
process.env.SHOPIFY_KEY);
这与如何在CommonJS中模拟ES6模块以及如何导入模块有关。你可以在这里阅读。