[SOLVED] Aggregation works from within cloud console but not when invoking cloud function

I am a bit lost here, I have an aggregation that has a Date comparison inside the match operator. Whenever I run this aggregation from inside the console I get a result, when I exchange the manually β€œnew Date()” with a param I pass to the cloud function, the result is undefined. What am I doing wrong?

Example from console that works (manually set with new Date()

const pipeline = [
  {
    lookup: {
      from: "_AddressSyncStatus",
      localField: "address",
      foreignField: "address",
      as: "synced_address"
    }
  },
  {
    unwind: {
      path: "$synced_address"
    }
  },
  {
    match: {
      $and: [
        {
          "synced_address.value": {
            $gte: 0
          }
        },
        {
          "synced_address.value": {
            $lte: 1000000000000000
          }
        },
        {
          "updatedAt": { $lte: new Date()}
        }
      ]
    }
  },
  {
    sort: {
      updatedAt: -1
    }
  },
  {
    group: {
      objectId: "$address",
      balance: {
        $last: "$balance"
      }
    }
  },
  {
    unwind: {
      path: "$balance"
    }
  },
  {
    group: {
      objectId: "$balance.symbol",
      count: {
        $sum: {
          $toDecimal: "$balance.balance"
        }
      }
    }
  },
  {
    project: {
      _id: 0,
      symbol: "$_id",
      total: "$count"
    }
  }
];


const query = new Parse.Query("TokenBalance");
const result = await query.aggregate(pipeline);
console.log(result);

Cloud function I want to use for this

Moralis.Cloud.define("getCombinedTokenBalances", async request => {
  const { from_date, min_eth_value, max_eth_value } = request.params;
  logger.info(
    "getCombinedTokenBalances " +
      JSON.stringify({ minutes_ago, min_eth_value, max_eth_value })
  );
  try {
    let result;
    let pipeline;
    const query = new Moralis.Query("TokenBalance");
    if (from_date) {
     
      pipeline = combined_tokenbalances_by_date_pipeline(
        from_date,
        min_eth_value,
        max_eth_value
      );
    } else {
      pipeline = current_combined_tokenbalances_pipeline(
        min_eth_value,
        max_eth_value
      );

      return await query.aggregate(pipeline);
    }
  } catch (error) {
    logger.error(error);
  }
});

the function to retrieve the pipeline

const combined_tokenbalances_by_date_pipeline = (
  from_date,
  min_eth_value,
  max_eth_value
) => [
  {
    lookup: {
      from: "_AddressSyncStatus",
      localField: "address",
      foreignField: "address",
      as: "synced_address"
    }
  },
  {
    unwind: {
      path: "$synced_address"
    }
  },
  {
    match: {
      $and: [
        {
          "synced_address.value": {
            $gte: min_eth_value
          }
        },
        {
          "synced_address.value": {
            $lte: max_eth_value
          }
        },
        {
          "updatedAt": { $lte: from_date}
        }
      ]
    }
  },
  {
    sort: {
      updatedAt: -1
    }
  },
  {
    group: {
      objectId: "$address",
      balance: {
        $last: "$balance"
      }
    }
  },
  {
    unwind: {
      path: "$balance"
    }
  },
  {
    group: {
      objectId: "$balance.symbol",
      count: {
        $sum: {
          $toDecimal: "$balance.balance"
        }
      }
    }
  },
  {
    project: {
      _id: 0,
      symbol: "$_id",
      total: "$count"
    }
  }
];

Logs show that I pass in the correct values (date and 2 numbers)

2022-03-24T12:51:36.722Z - getCombinedTokenBalances {"from_date":"2022-03-24T12:51:37.037Z","min_eth_value":0,"max_eth_value":100000000}

can you try to add use master key parameter?

if you remove that date part it works fine?

yes if I remove the date part completely it runs fine, if I manually just set a date with new Date() it runs fine. Only when I invoke the cloudfunction with a date parameter it doesnt work, result is undefined.

and when you invoke a cloud function without that date part it works fine?

yes when I invoke it without the date parameter it goes into the β€œelse” block which is the same aggregation just without the date match and that returns results as well fine.

when I log the pipeline from within the cloud function it looks like this, I dont see anthing wrong about the date part there, maybe you do.

pipeline: [{"lookup":{"from":"_AddressSyncStatus","localField":"address","foreignField":"address","as":"synced_address"}},{"unwind":{"path":"$synced_address"}},{"match":{"$and":[{"synced_address.value":{"$gte":0}},{"synced_address.value":{"$lte":1000000000}},{"updatedAt":{"$lte":"2022-03-24T13:13:00.234Z"}}]}},{"sort":{"updatedAt":-1}},{"group":{"objectId":"$address","balance":{"$last":"$balance"}}},{"unwind":{"path":"$balance"}},{"group":{"objectId":"$balance.symbol","count":{"$sum":{"$toDecimal":"$balance.balance"}}}},{"project":{"_id":0,"symbol":"$_id","total":"$count"}}]

Mate Im sorry I hope you dont wasted too much time. I have the return inside the else block instead below it…

1 Like