Getting Find() or findOne() working on the client side

Hello, so I’m a bit confused why what I’m trying to do isn’t working. Basically I am trying to check if from the current subscribed list of WeeklyCopays if a weekStartDate within the last 5 Sundays has already been created, if not then add to the list dateList. Code below showing what I am trying to do.

However the “var hold = WeeklyCopays.findOne({weekStartDate: d});” always returns nothing, even when I know there is a weekStartDate with that date. But what confuses me even more is that “this.copays = WeeklyCopays.find({}, { sort: { weekStartDate: -1 } });” from the code below does work and returns a list of WeeklyCopays.

Am still very new to all of this and have no idea what I maybe doing wrong here. Or if there is a better way to accomplish what I am trying to do.

export class CopaysList extends MeteorComponent {
    
    copays: Mongo.Cursor<Object>;
    dateList = [];

    constructor(params: RouteParams) {
        super();
        this.autorun(() => {
            this.subscribe('user.WeeklyCopays', params.get('userId'), () => {
                this.copays = WeeklyCopays.find({}, { sort: { weekStartDate: -1 } });
            }, true);
        });
        this.loadDateList();
    }
     
     loadDateList(){
         for (var index = 0; index < 5; index++) {
             var d = new Date();
             d = moment().day(index * -7).toDate();
             var hold =  WeeklyCopays.findOne({weekStartDate: d});
             
             if(!hold)
             {
                this.dateList[index] = d;
             }
         }
     }
}

Thanks

Just a tip, including the info about using Blaze Components in your question will save people unfamiliar with this package from trying to guess why this code looks like this. :wink:

Try:

     loadDateList(){
         for (var index = 0; index < 5; index++) {
             var d = new Date();
             d = moment().day(index * -7).toDate();
             console.log("d is: " + d + " and the result is " + JSON.stringify(WeeklyCopays.find({weekStartDate: d}));

What does console show?

1 Like

Thanks for the quick reply.

And what I get from one of the console logs is this below, which as you can see I am also using simpleSchema to help build the collections.

d is: Sun Feb 21 2016 21:20:26 GMT-0500 (Eastern Standard Time) and the result is {"collection":{"name":"WeeklyCopays","_docs":{"_map":{}},"_observeQueue":{"_tasks":[],"_running":false,"_runTimeout":null},"next_qid":1,"queries":{},"_savedOriginals":null,"paused":false,"_c2":{"_simpleSchema":{"_schema":{"userId":{"regEx":{},"optional":false},"weekStartDate":{"denyUpdate":true,"optional":false},"copay":{"defaultValue":0,"min":0,"optional":true},"pettyCash":{"defaultValue":0,"min":0,"optional":true},"refunds":{"defaultValue":0,"min":0,"optional":true}},"_schemaKeys":["userId","weekStartDate","copay","pettyCash","refunds"],"_autoValues":{},"_blackboxKeys":[],"_validators":[null],"_messages":{},"_depsMessages":{"_dependentsById":{}},"_depsLabels":{"userId":{"_dependentsById":{}},"weekStartDate":{"_dependentsById":{}},"copay":{"_dependentsById":{}},"pettyCash":{"_dependentsById":{}},"refunds":{"_dependentsById":{}}},"_firstLevelSchemaKeys":["userId","weekStartDate","copay","pettyCash","refunds"],"_objectKeys":{},"_validationContexts":{}}}},"sorter":null,"matcher":{"_paths":{"weekStartDate":true},"_hasGeoQuery":false,"_hasWhere":false,"_isSimple":true,"_selector":{"weekStartDate":"2016-02-22T02:20:26.789Z"}},"_transform":null,"reactive":true}

OK, so from what I see, the find() result is empty, so it just doesn’t find documents that have weekStartDate looking like d at all. I’d guess it’s a problem of incompatible date formats.

For now, we have:

d = Sun Feb 21 2016 21:20:26 GMT-0500 (Eastern Standard Time)

which becomes a selector:

"weekStartDate":"2016-02-22T02:20:26.789Z"

and that is the selector your database document has to match.

Now, can you give an example of a document? How does the weekStartDate exactly look like?

Or just do:

     loadDateList(){
         for (var index = 0; index < 5; index++) {
             var d = new Date();
             d = moment().day(index * -7).toDate();
             console.log("d is: " + d + " and the result is " + JSON.stringify(WeeklyCopays.findOne({}));
1 Like

Well with just WeeklyCopays.findOne({}) I was getting the error “undefined”, but then I tried moving this.loadDateList() to the same place I do this.copays = WeeklyCopays.find({}, { sort: { weekStartDate: -1 } }) and then it was finding WeeklyCopay documents. Messing around with this it seems like this.loadDateList() was getting called before WeeklyCopays was actually loaded.

However even after moving this.loadDateList() the findOne still returns empty. Below is what I get when I do console.log("d is: " + d + " and the result is " + JSON.stringify(WeeklyCopays.find({weekStartDate: d})) after I moved this.loadDateList().

Edit: After posting this and looking at the date in the collection, mainly 2016-02-21T05:00:00.000Z and the date I am checking for 2016-02-21T22:25:50.160Z, I see that the format and date is the same, but the time is different, so guessing that may have something to do with it.

d is: Sun Feb 21 2016 17:25:50 GMT-0500 (Eastern Standard Time) and the result is {"collection":{"name":"WeeklyCopays","_docs":{"_map":{"f8JSGrdpA5wTaaf8s":{"_id":"f8JSGrdpA5wTaaf8s","copay":300,"pettyCash":34,"refunds":56,"userId":"kkDXFxM4aoQ3RjtJ4","weekStartDate":"2016-02-21T05:00:00.000Z"},"GtTNYkcnBkBhmZ9Zt":{"_id":"GtTNYkcnBkBhmZ9Zt","copay":400,"pettyCash":12,"refunds":100,"userId":"kkDXFxM4aoQ3RjtJ4","weekStartDate":"2016-02-14T05:00:00.000Z"},"PPvXh4JW5zvTHbKq4":{"_id":"PPvXh4JW5zvTHbKq4","copay":350,"pettyCash":10,"refunds":0,"userId":"kkDXFxM4aoQ3RjtJ4","weekStartDate":"2016-02-07T05:00:00.000Z"}}},"_observeQueue":{"_tasks":[],"_running":false,"_runTimeout":null},"next_qid":1,"queries":{},"_savedOriginals":null,"paused":false,"_c2":{"_simpleSchema":{"_schema":{"userId":{"regEx":{},"optional":false},"weekStartDate":{"denyUpdate":true,"optional":false},"copay":{"defaultValue":0,"min":0,"optional":true},"pettyCash":{"defaultValue":0,"min":0,"optional":true},"refunds":{"defaultValue":0,"min":0,"optional":true}},"_schemaKeys":["userId","weekStartDate","copay","pettyCash","refunds"],"_autoValues":{},"_blackboxKeys":[],"_validators":[null],"_messages":{},"_depsMessages":{"_dependentsById":{}},"_depsLabels":{"userId":{"_dependentsById":{}},"weekStartDate":{"_dependentsById":{}},"copay":{"_dependentsById":{}},"pettyCash":{"_dependentsById":{}},"refunds":{"_dependentsById":{}}},"_firstLevelSchemaKeys":["userId","weekStartDate","copay","pettyCash","refunds"],"_objectKeys":{},"_validationContexts":{}}}},"sorter":null,"matcher":{"_paths":{"weekStartDate":true},"_hasGeoQuery":false,"_hasWhere":false,"_isSimple":true,"_selector":{"weekStartDate":"2016-02-21T22:25:50.160Z"}},"_transform":null,"reactive":true}

So yes it had to do with the fact that the dates were the same, but the times were not. After searching and finding this http://stackoverflow.com/questions/11973304/mongodb-mongoose-querying-at-a-specific-date I have changed my code to what I have below as well as moving loadDateList() to get called after WeeklyCopay is loaded and now it’s working.

Thanks brajt for the help and for helping me to figure out how to debug this.

    loadDateList(){
             var i = 0;
             for (var index = 0; index < 5; index++) {
                 var start = moment(moment().day(index * -7)).startOf('day');
                 var end = moment(start).add(1, 'days');
                 var hold =  WeeklyCopays.findOne({weekStartDate: 
                     {"$gte": start.toDate(), "$lt": end.toDate()}});
                 
                 if(!hold)
                 {
                    this.dateList[i] = start.toDate();
                    i++;
                 }
             }
         }
1 Like