[英]Meteor: Regex not working for selector of aldeed:tabular
我正在使用Meteor和aldeed:tabular包来显示“联系人”集合的表。 我在表格上方从A到Z设置了按钮,以便我可以选择一个字母,并且仅列出“ firstName”以所选字母开头的联系人。 我使用表的“选择器”属性来执行此操作。
{{> tabular table=ContactsTable.Contacts id="contactsTableID" selector=selector class="table table-bordered table-condensed table-striped"}}
当我单击一个字母时,我将一个值存储到这样的会话变量中...
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.
}
}
});
使用以上语法,单击字母时,服务器上出现以下错误...
子tabular_getInfo id hyD5bp2r6F2YuzoMa的异常MongoError:无法规范化查询:BadValue $ regex必须为字符串
因此,如果我尝试将语法更改为此,则错误消失了,但是单击字母不会在表上返回任何内容。
return {'firstName': {'$regex': '/^a/gi'}};
我也尝试了以下语法...
return {firstName:/^a/gi};
return {firstName: {'$regex': '/^a/', '$options': 'i'}}
显然第二个应该工作,但这不适合我。
我还验证了通过在控制台中使用它肯定应该返回的数据(我看到返回了5个文档)...
Contacts.find( { "firstName": { "$regex": /^a/gi } } ).fetch()
谢谢
在流星论坛上回答了这个问题... https://forums.meteor.com/t/regex-not-working-for-selector-of-aldeed-tabular/17244/2?u=serks
保持正则表达式为字符串而不包含正斜杠,因此应执行以下操作:
return {
firstName: {
$regex: '^a',
$options: 'i'
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.