Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Unrecognized expression '$dateTrunc' #62

Open
apitlekays opened this issue Apr 16, 2022 · 1 comment
Open

Error: Unrecognized expression '$dateTrunc' #62

apitlekays opened this issue Apr 16, 2022 · 1 comment

Comments

@apitlekays
Copy link

I use this package to create an aggregation of a huge data coming from 'https://publicinfobanjir.water.gov.my/wp-content/themes/enlighten/data/latestreadings.json', trimming the data to aggregate the latest (current day) record and publish it via the Meteor publish. I encountered two errors i.e. one that days 'unrecognised expression' and the other on something about sanitization and reported to the client as an error.

Situation: (Refer to 'Current Result' section below for details on the errors):

  • Error 1 occurs on both localhost and on Meteor Galaxy.
  • Error 2 only occurs on Meteor Galaxy, works fine on localhost.

Context

The system that I am building is showing current rainfalls and river water levels in Malaysia. The data is siphoned JSON endpoint from 'https://publicinfobanjir.water.gov.my/wp-content/themes/enlighten/data/latestreadings.json' and the data is fetched using meteor/fetch module, every 15 minutes (cron job using wildhart:jobs). The data is then saved into MongoDB. I then created a publish function to have reactive access to the data, and I used tunguska-reactive-aggregate package in the publish function. After that, I subscribe to the publication on the frontend via React Context API.

Process

  1. The publication function (link to SO original answer, which I tweaked to fit my use case):
//server/main.js
Meteor.publish('PIBDataAlerts', function(){
      ReactiveAggregate(this, PIBLatest, [
        {
          $match: {
            //'stationStatus' : 'ON',
            $expr: {
              $and: [
                {
                  $ne : [
                    {
                      $filter: {
                        input: "$waterLevelData",
                        as:'w',
                        cond: {
                          $eq: [
                            {
                              $dateTrunc: {
                                date: "$$w.wlDateTime",
                                unit: "day"
                              }
                            },
                            {
                              $dateTrunc: {
                                date: "$$NOW",
                                unit: "day"
                              }
                            }
                          ]
                        }
                      }
                    },[]
                  ]
                },
              ]
            }
          }
        },
        {
          $set: {
            waterLevelHFZ: {
              $filter: {
                input: "$waterLevelData",
                as: 'w',
                cond: {
                  $and: [
                    {
                      $in: [
                        "$$w.wlSeverity",
                        [
                          "Alert",
                          "Warning",
                          "Danger"
                        ]
                      ]
                    },{
                      $eq : [
                        "$$w.wlDateTime",
                        {
                          $max: "$waterLevelData.wlDateTime"
                        }
                      ]
                    },{
                      $eq: [
                        {
                          $dateTrunc: {
                            date: "$$w.wlDateTime",
                            unit: "day"
                          }
                        },{
                          $dateTrunc: {
                            date: "$$NOW",
                            unit: "day"
                          }
                        }
                      ]
                    }
                  ]
                }
              }
            },
            rainfallDataHFZ : {
              $filter: {
                input: "$rainfallData",
                as:'r',
                cond: {
                  $and: [
                    {
                      $eq: [
                        "$r.rfDateTime",
                        {
                          $max: "$rainfallData.rfDateTime"
                        }
                      ]
                    },{
                      $eq: [
                        {
                          $dateTrunc : {
                            date: "$$r.rfDateTime",
                            unit: "day"
                          }
                        },{
                          $dateTrunc : {
                            date: "$$NOW",
                            unit: "day"
                          }
                        }
                      ]
                    }
                  ]
                }
              }
            }
          }
        },
        {
          $project : {
            "stationId": 1,
            "stationName": 1,
            "lat": 1,
            "long": 1,
            "district": 1,
            "state": 1,
            "wlNormal": 1,
            "wlAlert": 1,
            "wlWarning": 1,
            "wlDanger": 1,
            "stationStatus": 1,
            "waterLevelHFZ" : 1,
            "rainfallDataHFZ" : 1
          }
        }
      ]);
    })
  1. Subscription code:
//PIBContext.js
import React, { useContext, useState, useEffect } from 'react';
import { Meteor } from 'meteor/meteor';
import { withTracker } from 'meteor/react-meteor-data';
import { PIBLatest } from './../imports/api/PIBLatest';

const PIBLatestContext = React.createContext();

export function usePIBLatest(){
    return useContext(PIBLatestContext);
}

const PIBLatestProvider = (props, { children }) => {
    const [ PIBData, setPIBData ] = useState();

    useEffect(() => {
        const getData = () => {
            let mounted = true;
            if(props != undefined) {
                setPIBData(props.actualData)
            }
            return function cleanup(){
                mounted = false;
            }
        }
        getData();
    }, [props])

    return (
        <PIBLatestContext.Provider value={PIBData}>
            { props.children }
        </PIBLatestContext.Provider>
    )
}
export default withTracker((props) => {
    const subscribe = Meteor.subscribe('PIBDataAlerts');
    const loading = !subscribe.ready();
    const data = PIBLatest.find();
    const dataExist = !loading && !!data;

    return {
        loading,
        dataExist,
        actualData: PIBLatest.find().fetch()
    }
})(PIBLatestProvider)

Expected result

The result should be an array of objects that contains the aggregated data.

Current result

Instead, I encountered these error messages in the console:

Error 1.

Sanitized and reported to the client as: Error: tunguska:reactive-aggregate [Error]
xzxt32022-04-16 13:38:07+08:00 at errorClass.<anonymous> (packages/tunguska:reactive-aggregate/aggregate.js:11:27)
xzxt32022-04-16 13:38:07+08:00 at new errorClass (packages/meteor.js:660:17)
xzxt32022-04-16 13:38:07+08:00 at update (packages/tunguska:reactive-aggregate/aggregate.js:267:13)
xzxt32022-04-16 13:38:07+08:00 at ReactiveAggregate (packages/tunguska:reactive-aggregate/aggregate.js:334:3)
xzxt32022-04-16 13:38:07+08:00 at Subscription.<anonymous> (server/main.js:151:7)
xzxt32022-04-16 13:38:07+08:00 at packages/matb33:collection-hooks/server.js:33:71
xzxt32022-04-16 13:38:07+08:00 at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1257:12)
xzxt32022-04-16 13:38:07+08:00 at Subscription._handler (packages/matb33:collection-hooks/server.js:33:26)
xzxt32022-04-16 13:38:07+08:00 at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1885:12)
xzxt32022-04-16 13:38:07+08:00 at packages/ddp-server/livedata_server.js:1107:9
xzxt32022-04-16 13:38:07+08:00 at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1257:12)
xzxt32022-04-16 13:38:07+08:00 at Subscription._runHandler (packages/ddp-server/livedata_server.js:1106:60)
xzxt32022-04-16 13:38:07+08:00 at Subscription.subscriptionProto._runHandler (packages/mdg:meteor-apm-agent/lib/hijack/wrap_subscription.js:12:24)
xzxt32022-04-16 13:38:07+08:00 at Session._startSubscription (packages/ddp-server/livedata_server.js:917:9)
xzxt32022-04-16 13:38:07+08:00 at Session.sub (packages/ddp-server/livedata_server.js:673:12)
xzxt32022-04-16 13:38:07+08:00 at packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:77:34
xzxt32022-04-16 13:38:07+08:00 at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1257:12)
xzxt32022-04-16 13:38:07+08:00 at Session.sessionProto.protocol_handlers.sub (packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:76:44)
xzxt32022-04-16 13:38:07+08:00 at packages/ddp-server/livedata_server.js:603:43

and Error 2.

Exception from sub PIBDataAlerts id Q7QrEg8nzQdesitib Error: Unrecognized expression '$dateTrunc'xzxt3
2022-04-16 13:38:07+08:00 at update (packages/tunguska:reactive-aggregate/aggregate.js:267:13)xzxt3
2022-04-16 13:38:07+08:00 at ReactiveAggregate (packages/tunguska:reactive-aggregate/aggregate.js:334:3)xzxt3
2022-04-16 13:38:07+08:00 at Subscription.<anonymous> (server/main.js:151:7)xzxt3
2022-04-16 13:38:07+08:00 at packages/matb33:collection-hooks/server.js:33:71xzxt3
2022-04-16 13:38:07+08:00 at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1257:12)xzxt3
2022-04-16 13:38:07+08:00 at Subscription._handler (packages/matb33:collection-hooks/server.js:33:26)xzxt3
2022-04-16 13:38:07+08:00 at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1885:12)xzxt3
2022-04-16 13:38:07+08:00 at packages/ddp-server/livedata_server.js:1107:9xzxt3
2022-04-16 13:38:07+08:00 at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1257:12)xzxt3
2022-04-16 13:38:07+08:00 at Subscription._runHandler (packages/ddp-server/livedata_server.js:1106:60)xzxt3
2022-04-16 13:38:07+08:00 at Subscription.subscriptionProto._runHandler (packages/mdg:meteor-apm-agent/lib/hijack/wrap_subscription.js:12:24)xzxt3
2022-04-16 13:38:07+08:00 at Session._startSubscription (packages/ddp-server/livedata_server.js:917:9)xzxt3
2022-04-16 13:38:07+08:00 at Session.sub (packages/ddp-server/livedata_server.js:673:12)xzxt3
2022-04-16 13:38:07+08:00 at packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:77:34xzxt3
2022-04-16 13:38:07+08:00 at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1257:12)xzxt3
2022-04-16 13:38:07+08:00 at Session.sessionProto.protocol_handlers.sub (packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:76:44)xzxt3
2022-04-16 13:38:07+08:00 at packages/ddp-server/livedata_server.js:603:43
@robfallows
Copy link
Owner

What version of Meteor are you using (in dev and production)? The MongoDB documentation for $dateTrunc states that it is new in v5, which is only supported in Meteor v2.6 and above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants