Moralis API sort query numerically instead of 0, 10, 100, 1001, 1002

183 Views Asked by At

This is for the Moralis API to make Ethereum apps. I am using getAllTokenIds() to pull data for an NFT collection, and storing the data in Moralis database.

using this line: query.ascending(“tokenId”);

my results are coming in with the token ids (0, 1, 10, 100, 1000, 10000, 10001, 10002, 10003 …) how do I make it so my order for token Ids is (0, 1, 2, 3, 4, 5 …)?

I have tried to look up ways to convert my token Ids columns into number format, but I am not sure if there is a way to do it in the Moralis database.

1

There are 1 best solutions below

0
On

GM there,

usually the tokenId returned will be string, so it will order it like a string

So one solutions would be after you pushed the result to Moralis DB, run beforeSave or afterSave to make a new column that save the integer version of the tokenIds

Alternatively, convert those tokenIds to integer first before pushing then to Moralis DB

Let me know if you found something else

Cheers~