I haven’t tested this code, and using Blaze has been a while for me. But it should work. Maybe you need to use Template.currentData(); in the helper though.
'registeredatcheck': function(registered){
var registered = Logins.find().forEach(function(check) {
if (isNaN(check.RegisteredAt)) {
return true;
}
});
But it still doesn’t display YES (or anything) in the data context (the other fields work) -
{{#each getlogins}}
//lines skipped
{{#if registeredatcheck}} YES {{/if}}
{{/each}}
Also, my UI currently renders 3000 rows. This function is causing it slow down to the point that it crashes on Chrome. Is there a simpler way to check for NaN without doing a forEach?
Ah, my bad. I focused entirely on the logic of the test and not on the surrounding code. You cannot return a result from the helper by doing a return from inside a callback (forEach): that just returns from the callback. You need to restructure your helper to return a cursor (or an array):
registeredatcheck(registered) {
var registered = Logins.find({RegisteredAt: NaN});
}
Note: using NaN in a query is supported in MongoDB, but I’ve not tested it in minimongo. Let me know!
Now, this returns true. Can I switch the ==‘Aug 8 2016 8:08AM’ to test for either NaN or data type==date? Testing for NaN using isNaN(this.RegisteredAt) returns true for all documents.
I guess you have some serious typing issues in your database. If that check doesn’t work, than the date is not stored as an Date type.
Alternatives you can use are:
// check if it is not a NaN
return isNaN(this.RegisteredAt) === false;
// try parse to a date, if it works, it's a valid date
return Date.parse(this.RegisteredAt) instanceof Date;
// parsing returns NaN if it doesn't work. Check if it didn't work.
return isNaN(Date.parse(this.RegisteredAt)) === false;
// or really wrong, as it seams you're storing dates as strings
return typeof this.RegisteredAt === 'string'
@smeijer So it looks like the date is a string and it works with that test. Thanks for that - that was useful for debugging. I will check the Python script that sends down this date value.