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}