googleapis/google-cloud-node

[Retail API] ProductServiceClient.listProductsAsync() pagination not working

BitoSaga opened this issue · 1 comments

My code is like this:

`const productClient = new ProductServiceClient({
'credentials': credentials,
'autoPaginate': false, // I add this according to some other issue previously reported
});

async function listProducts() {
// Construct request
const request = {
'parent': 'projects/xxxx/locations/global/catalogs/default_catalog/branches/0',
'pageSize': 4,
'autoPaginate': false, // add this again to ensure it's effective because it's not documented
};

// Run request
const iterable = productClient.listProductsAsync(request);
const result = [];

for await (const response of iterable) {
result.push(response);
}
return result;
}
`

However, I always get the full list of all products no matter pageSize. Please help if anyone knows the solution.

for those who are interested, async method will return an iterable that may return all items without additional control. I am still learning how to make it work to return a page of items each time. but for non async method, like listProducts(), you can add {autoPaginate:false} as the 2nd argument to the method call, like productClient.listProducts(request, {autoPaginate:false}). It works as expected. Please refer to: Auto Pagination. I will close this issue.