简体   繁体   English

流星:正则表达式不适用于aldeed:tabular选择器

[英]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. 我正在使用Meteor和aldeed:tabular包来显示“联系人”集合的表。 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. 我在表格上方从A到Z设置了按钮,以便我可以选择一个字母,并且仅列出“ firstName”以所选字母开头的联系人。 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 contacts.html

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

contacts.js 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 子tabular_getInfo id hyD5bp2r6F2YuzoMa的异常MongoError:无法规范化查询:BadValue $ regex必须为字符串

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)... 我还验证了通过在控制台中使用它肯定应该返回的数据(我看到返回了5个文档)...

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 在流星论坛上回答了这个问题... 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'
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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