简体   繁体   中英

Meteor: Regex not working for selector of aldeed:tabular

I am using Meteor and 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

This question was answered on the Meteor forums... https://forums.meteor.com/t/regex-not-working-for-selector-of-aldeed-tabular/17244/2?u=serks

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM