phillbaker / digitalocean-node

Unofficial node client for the v2 DigitalOcean API
http://phillbaker.github.io/digitalocean-node
MIT License
67 stars 16 forks source link

Pagination promise example error #38

Closed milekz closed 2 years ago

milekz commented 2 years ago

Hi,

I started to play with node+DO and found issue with one of examples from main doc. Promise style generate a bug TypeError: Cannot read properties of undefined (reading 'body') - droplets object don't have ._digitalocean.body.links data path

milekz commented 2 years ago

fixed version

function getAllDroplets() {
  var allDroplets = [];

  function getDropletPage(page) {
    if (page == null) {
      page = 1;
    }

    return client.droplets.list(page)
      .then(function(droplets) {
        droplets.forEach(droplet => allDroplets.push(droplet));
        var links = droplets._digitalOcean.body.links;
        var isLastPage = links && (
          !links.pages ||
            (links.pages && links.pages.last === undefined)
        );

        if (isLastPage) {
          return allDroplets;
        } else {
          return getDropletPage(page + 1);
        }
      });
  }

  return getDropletPage();
}
phillbaker commented 2 years ago

Thanks, would you be able to make a PR with this fix?

On Tue, May 3, 2022 at 8:24 AM Miłosz Szewczak @.***> wrote:

fixed version

` function getAllDroplets() { var allDroplets = [];

function getDropletPage(page) { if (page == null) { page = 1; }

return client.droplets.list(page) .then(function(droplets) { droplets.forEach(droplet => allDroplets.push(droplet)); var links = droplets._digitalOcean.body.links; var isLastPage = links && ( !links.pages || (links.pages && links.pages.last === undefined) );

if (isLastPage) {
  return allDroplets;
} else {
  return getDropletPage(page + 1);
}

});

}

return getDropletPage(); } `

— Reply to this email directly, view it on GitHub https://github.com/phillbaker/digitalocean-node/issues/38#issuecomment-1116038867, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAXCKORDXO2745ZGZIC57TVIELGRANCNFSM5U6WDPTQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

milekz commented 2 years ago

PR created