首页 > 解决方案 > 是否可以在一次调用中访问多个产品变体信息?Shopify API 节点

问题描述

我正在使用 shopify-api-node 包,该包在使用 Shopify 的 api 时非常流行。一切都很顺利,但是当尝试一次调用多个产品变体时,似乎出现了问题。调用产品变体时,它需要我在尝试调用变体之前检索到的产品 ID。当尝试一次调用多个变体上的一组 id 时,我收到一条错误消息,上面写着“(节点:4628)UnhandledPromiseRejectionWarning:HTTPError:响应代码 414(请求 URI 太大)”。我还尝试使用 .join() 用逗号分隔 id,但这样做之后它只返回第一个 id 的变体,而忽略其余的。任何帮助都会很棒!

shopify 节点包:https ://www.npmjs.com/package/shopify-api-node

shopify api 产品变体文档:https ://shopify.dev/docs/admin-api/rest/reference/products/product-variant#index-2021-04

const Shopify = require('shopify-api-node');
const shopify = new Shopify({
shopName: 'removed',
apiKey: 'for',
password: 'post'
});
async function callReq(val) {
    let params = { limit: 10 };

    // first do while loop is to access all of the product ids
    do {
        
        const products = await shopify.product.list(params);
        var getIds = (JSON.parse(JSON.stringify(products), function(key, value) {
            (key == 'id') ? idArray.push(value): null;
        }));
        
        await new Promise(resolve => setTimeout(resolve, 500));
        params = products.nextPageParameters;
        console.log(idArray.length)
        
    } while (params !== undefined);
    // Second do while loop is for accessing the product variants using the ids gathered prior
    do{
    let params = { limit:250 };
        
        // problem is here when trying to list the product variants I'm only able to list one at a 
        time rather than calling multiple by separating each id with a comma as stated in shopify 
        api 
        const variants = await shopify.productVariant.list(idArray, params);
        console.log(JSON.stringify(variants));
        var getSkus = (JSON.parse(JSON.stringify(variants), function(key, value) {
            (key == 'sku') ? value != null ? skuArray.push(value) : null : null;
            (key == 'price') ? costArray.push(value): null;
        }));
    
     }
        
    }while(params !== undefined);
    }
}

标签: javascriptnode.jsapishopify

解决方案


推荐阅读