Shopify REST API 使用分页来显示产品。每页的默认产品数量为 50 个,但限制可以设置为 250 个。必须使用上一个响应中提供的下一页链接多次调用 API。
我的问题是我无法让我的代码对〜1,300个产品进行分页并将它们连接到单个数组
allProducts
。如下图 response.json
所示,产品的首页显示正确,但任何后续页面都显示不正确。我试图定义链接:
const headerLink = products.headers.Link[0]
response.json
具有以下模式,因此 headerLink
:
[
{
"data": [
{
"id": 81226235887,
"title": "title",
.....
],
"headers": {
...
"Link": [
"<https://{EXAMPLE}.com/admin/api/2023-04/products.json?limit=50&page_info=yJsYXN0X2lkIjo4MTIyNDIxNzcyNTkxLCJsYXN0X3ZhbHsafwegVlIjoiMTRLIFJvc2UgNSBtbSBDdXNoaW9JKDAuIEZvmluZyIsImRpcmVsdawejdGlvbiI6Im5leHQifQ>; rel=\"next\""
],
...
}
}
}
}
]
下面我的代码中的rel=\"next\""
表示还有另一个页面可用。如果我们已经到达最后一页,则响应中将缺少此行。我的代码在中引用了它
const match = headerLink.match(/<[^;]+\/(\w+\.json[^;]+)>;\srel="next"/);
我的代码似乎反复吐出相同的链接:
The next link: https://example.myshopify.com/admin/api/2023-04/products.json?limit=50&page_info=yJsYXN0X2lkIjo4MTIyNDIxNzcyNTkxLCJsYXN0X3ZhbHsafwegVlIjoiMTRLIFJvc2UgNSBtbSBDdXNoaW9JKDAuIEZvmluZyIsImRpcmVsdawejdGlvbiI6Im5leHQifQ
我的代码:
/**
* FILEPATH: /Users/christopherkonicki/Documents/GitHub/Import_Stuller_Nivoda/deleteMe.js
*
* This script imports all products from a Shopify store using the Shopify REST API.
* It uses the 'shopify' and 'session' objects from the 'testingShopifyApp.js' file.
*
* The 'makeRequest' function makes a request to the Shopify API to get all products.
* It uses pagination to get all products, since the API returns a maximum of 250 products per request.
*
* The 'allProducts' array stores all products retrieved from the API.
* The 'since_id' variable is used to keep track of the last product ID retrieved from the API.
*
* The function returns a Promise that resolves with the 'allProducts' array when all products have been retrieved.
*/
// In another .js file in the same directory
import { shopify } from './testingShopifyApp.js';
import { session } from './testingShopifyApp.js';
import "@shopify/shopify-api/adapters/node";
const client = new shopify.clients.Rest({session});
let allProducts = [];
let since_id = 0;
/**
* This function makes a request to the Shopify API to get all products.
* It uses pagination to get all products, since the API returns a maximum of 250 products per request.
*
* The 'nextLink' parameter is used to specify the URL of the next page of products to retrieve.
* If no 'nextLink' is provided, the function starts by retrieving the first page of products.
*
* The function returns a Promise that resolves with an array of all products retrieved from the API.
*/
function makeRequest(nextLink = 'https://{EXAMPLE}.com/admin/api/2023-04/products.json') {
return new Promise((resolve, reject) => {
let products = [];
// Make a request to the Shopify API to get the next page of products
fetch(nextLink).then(async r => {
// If this is the first page of products, use the Shopify REST API to get all products
if (since_id === 0) {
products = await shopify.rest.Product.all({
session,
query: {
since_id: 0,
fields: 'id,title,images',
},
});
allProducts = allProducts.concat(products);
console.log('All products so far: ', allProducts[0]);
};
// If this is not the first page of products, use the 'since_id' parameter to get all products since the last product retrieved
if (since_id !== 0) {
let headerLink = products.headers.Link[0];
while (headerLink) {
products = await shopify.rest.Product.all({
session,
query: {
since_id: since_id,
fields: 'id,title,images',
},
});
allProducts = allProducts.concat(products);
console.log('All products so far: ', allProducts[0]);
headerLink = products.headers.Link[0];
}
}
// Get the URL of the next page of products from the 'Link' header of the API response
const headerLink = products.headers.Link[0];
const match = headerLink.match(/<[^;]+\/(\w+\.json[^;]+)>;\srel="next"/);
const nextLink = match ? `https://{EXAMPLE}.com/admin/api/2023-04/${match[1]}` : false;
console.log('The next link: ', nextLink);
// If there is a next page of products, recursively call this function with the URL of the next page
if (nextLink) {
makeRequest(nextLink);
} else {
// If there are no more pages of products, resolve the Promise with the array of all products retrieved
resolve(allProducts[0]);
}
}).catch(reject);
});
}
// Call the 'makeRequest' function to start retrieving all products from the Shopify API
makeRequest();
为此特定目的,shopify 有一个内置的 graphql 解决方案批量查询