sphere-node-sdk
sphere-node-sdk copied to clipboard
Loading products using process function does not stop
Expected behavior
Function sphere.products.perPage(50).process(function process(res){}) should load products in batches and send them to process function.
Actual behavior
If I add this code to process function, loading will never stop.
var list = res.body.results;
list = list.map(function (item) {
delete item.id; // when commented out - loading works as expected
return item;
});
Steps to reproduce the behavior
I was testing it on:
- project with 639 products and perPage = 50
- project with 1 product and perPage = 1
It looks like it occurs when perPage <= productCount Here is full code which gives me an error.
var _ = require('lodash');
var Promise = require('bluebird');
var Sphere = require('sphere-node-sdk');
var SphereUtils = require('sphere-node-utils');
var SphereClient = Sphere.SphereClient;
var ProjectCredentialsConfig = SphereUtils.ProjectCredentialsConfig;
var sourceCredentials, sourceClient;
var PER_PAGE = 50;
var projectKey = "<product-key>";
var total = 0;
console.log('>> Loading products from %s', projectKey);
ProjectCredentialsConfig.create()
.then(function (credentials) {
sourceCredentials = credentials.enrichCredentials({project_key: projectKey});
sourceClient = new SphereClient({config: sourceCredentials});
console.log("Loading per page %d", PER_PAGE);
var total = 0;
return sourceClient.products
.perPage(PER_PAGE)
.process(function(res) {
var list = res.body.results;
total += list.length;
console.log('Loaded %d / totally %d', list.length, total);
list = list.map(function (item) {
delete item.id; // when commented out - loading works as expected
return item;
});
return Promise.resolve();
})
})
.then(function() {
console.log("Finished - total %d", total);
})
.catch(function (err) {
console.error("Final error:", err.stack || err);
});
Well, you are mutating the body result, that is used inside the SDK to find out where to continue the paged request. The id is the critical part here to do the paged request. You can write your map function as a pure function everything will work fine:
import omit from 'lodash/omit'
const listWithoutIds = list.map(item => omit(item, 'id'));