Inconsistent pagination with SDK when filtering products

javascript
products
#1

Hi :slight_smile:

I’m having some trouble with filtering and pagination.

Whenever I do a product get call via the SDK [ i.e. Moltin.Products.Filter(...).All ] the resultset has a random amount of duplicate products in it.

Example:

let _ = require('lodash')
const MoltinGateway = require("@moltin/sdk").gateway;

let gateway = {
    client_id: "8NNRJcFqSk7nkxrutpaNZpcKlBduaVHH8i9UV1WWXU",
};

async function getProductsWith() {
    let Moltin = MoltinGateway(gateway);
    let products = await Moltin.Products.Filter({eq: {"collection.id": '517fba26-5c17-4758-9f33-f4a1538a0233'}}).All().catch();
    while (products.data.length < products.meta.results.total) {
        let d = await Moltin.Products.Filter({eq: {"collection.id": '517fba26-5c17-4758-9f33-f4a1538a0233'}}).Offset(products.data.length).All().catch();
        products.data = products.data.concat(d.data)
    }
    return products.data;
}

for (let i = 0; i < 5; i++) {
    getProductsWith().then(function (response) {
        let deduped = _.uniqBy(response, "id");
        console.log("Unique items returned: ", deduped.length)
    });
}

Results in:

$ node demo.js
Unique items returned:  515
Unique items returned:  515
Unique items returned:  529
Unique items returned:  515
Unique items returned:  553

I’ve tried Sorting with the filter but it’s still not correct. Adding a desc sort seems to make it better (there’s less variance between the returned lengths. Interestingly, with .Sort("-sku") it seems to be correct?

Am I doing something wrong?

Thanks!

0 Likes

#2

Some more info: https://github.com/moltin/js-sdk/issues/249

Looks like an issue with the API as I was able to reproduce it with cURL calls

0 Likes

#3

Hi @nikolubbe

Thanks for the detailed report! We’ve replicated this and will look into what’s going on.

Thanks,
James

0 Likes

#4

Hey @nikolubbe

Just an update on this. You were on the right lines with the sort, because all products match the sort equally (they either do or do not exist in that category) the order in the responses is fairly random. As the order is potentially different for each response, it is possible you’ll get the same product in different positions in different responses.

We’ll add a default sort of newest first (we use this pattern for in other places, like orders already). In the meantime, as you noticed, adding a sort to the requests will work around the issue.

Thanks again for reporting!
James

0 Likes

#5

Thank you so much for the speedy response! Although in some case sorting does work around the issue, as I mentioned it’s not always correct.

But I’ll wait for the fix and check again :slight_smile: Thanks!

0 Likes