How to use sort parameter in getnftowners function?

In the documentation of getnftowners there is this note saying to use sort parm to sort token_id for consistent pagination results. However, I could not find this being used in any function.

Can anyone share syntax on how to use it and also if there is a way to ascend or decent results using sort.

I tried passing it as sort: "token_id" but it doesn’t seem to work this way.

1 Like

Hi another user on Discord had problems doing this as well.

I noticed that the documentation here doesn’t mention sorting/ordering at all so there is some conflicting info.

GetNFTTransfers has an order parameter, maybe that will work here.

getnftowners function is throwing an error when the order parameter is used. So we can’t use it here I guess.

Sort parameter was disabled some time ago because of performance reasons. The queries were too slow and were affecting the entire api performance.

2 Likes

You can sort the call results on your side using underscore.js:

const _ = require("underscore");
const Moralis = require("moralis/node");
const { serverUrl, appId } = require("./secrets.json");

const execute = async () => {
  await Moralis.start({ serverUrl, appId });
  var nftOwners = null;

  const options = {
    address: "0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB", // Cryptopunks address
    chain: "0x1",
    offset: 0,
  };

  nftOwners = await Moralis.Web3API.token.getNFTOwners(options);

  let data = nftOwners.result;
  let sortedData = _.sortBy(data, "token_id");
  console.log(sortedData);
};

execute();
1 Like

Can anyone expand on this secrets.json?? is that a moralis thing? and what secret goes there? lol sorry if its a bad question but this is where Im currently stumped trynig to add simple pagination to our nft marketplace

That line only sets server url and app id, you can hardcode them directly in your code

you can follow this example here to create a secrets.json file:

In short, it is just a replacement for dotenv variables if you are not willing to add an extra lib to your project.