Regex not working for selector of aldeed:tabular

I am using aldeed:tabular package to display a table for a ‘contacts’ collection. I have set up buttons from A to Z above the table so that I can select a letter and only list contacts who’s ‘firstName’ begins with the selected letter. I am doing this with the ‘selector’ attribute of the table.

{{> tabular table=ContactsTable.Contacts id="contactsTableID" selector=selector class="table table-bordered table-condensed table-striped"}}

When I click on a letter, I am storing a value into a session variable like this…

contacts.html

<button type="button" class="btn btn-default selectLetterA">A</button>

contacts.js

Template.contacts.events = {
'click .selectLetterA': function(e) { 
    e.preventDefault(); 
    Session.set('selectedLetter','a'); 
}
};

Template.contacts.helpers({
selector: function (){
    if (Session.get('selectedLetter') != '') {
        if (Session.get('selectedLetter') == 'all') {return {createdBy: Meteor.userId()}} else if 
        (Session.get('selectedLetter') == 'a') {return {firstName: {'$regex': /^a/i }}} else if
        (Session.get('selectedLetter') == 'b') {return {firstName: {'$regex': /^b/i }}} else if
        ...     
        }
    } else {
        return {createdBy: Meteor.userId()}; //If the selectedLetter is blank, return all contacts created by current user.
    }
}
});

Using the above syntax, I get the following error on the server when I click the letter…

Exception from sub tabular_getInfo id hyD5bp2r6F2YuzoMa MongoError: Can’t canonicalize query: BadValue $regex has to be a string

So if I try changing the syntax to this, the error goes away, but clicking on the letter returns nothing on the table.

return {'firstName': {'$regex': '/^a/gi'}};

I have also tried the following syntax…

return {firstName:/^a/gi};
return {firstName: {'$regex': '/^a/', '$options': 'i'}}

Apparently the second one is supposed to work but it’s not for me.

I have also verified that there should definitely be data returned by using this in the console (I see 5 documents returned)…

Contacts.find( { "firstName": { "$regex": /^a/gi } } ).fetch()

Thanks

Keep the regex a string without forward slashes, so something like the following should work:

return {
  firstName: {
    $regex: '^a',
    $options: 'i'
  }
}
1 Like

It works!!
I can’t believe I didn’t try that out of all the different ways I messed with it.
Thank you!