Coinflip Tutorial - Overflow Error

I am working through the Coinflip Tutorial (part 2) https://www.youtube.com/watch?v=Pj-X7sNyw2E

I am trying to run the following cloud function

Moralis.Cloud.define("biggestWinners", async function(request) {
  const query = new Parse.Query("flips");
  query.equalTo("win", true);
  const pipeline = [
    {
      group: {
        objectId: "$user",
        totalWon: { $sum: { $toInt: "$bet" }},
      },
    },
    { sort: {totalWon: -1}},
  ];

  // the master key is required for aggregate queries
  const pipelineresult = await query.aggregate(pipeline, { useMasterKey: true });

  return pipelineresult;

});

I am calling it from my web application using:

async function bigWinners(){
    let results = await Moralis.Cloud.run("biggestWinners", {});
    return results
}


let bigwinnerResults = await bigWinners()

But when I run it, i get the following errors.

On the browser console I get:

moralis.js:21595 POST https://ouxkgtx4ngla.moralis.io:2053/server/functions/biggestWinners 400 (Bad Request)

moralis.js:23697 Uncaught Error: Failed to parse number '1234567891234' in $convert with no onError value: Overflow
    at handleError (moralis.js:21728)
    at async bigWinners (main.js:104)
    at :8080/async <anonymous>:1

On the Moralis Dashboard Logs i get the following error

2021-06-01T07:44:49.129Z - MongoError: Failed to parse number '1234567891234' in $convert with no onError value: Overflow
    at MessageStream.messageHandler (/moralis-server/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:376:20)
    at MessageStream.emit (domain.js:470:12)
    at processIncomingData (/moralis-server/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/moralis-server/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at Socket.ondata (internal/streams/readable.js:745:22)
    at Socket.emit (events.js:376:20)
    at Socket.emit (domain.js:470:12)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
    at TCP.callbackTrampoline (internal/async_hooks.js:134:14)

Server version: 0.0.223
Server URL: https://ouxkgtx4ngla.moralis.io:2053/server
Server name: coinflip-tut

Try $toLong instead of $toInt

Reason - the number is too big to fit in Integer type, so we try Long type which can store bigger numbers

1 Like

Perfect! Thanks for that Ivan

1 Like