【问题标题】:Instance search in MeteorMeteor 中的实例搜索
【发布时间】:2015-07-31 15:14:00
【问题描述】:

我有以下数组:

UT.insert({
            "chatUsers": [{
                        firstName: "John",
                        lastName: "Doe"
                    }, {
                        firstName: "Anna",
                        lastName: "Smith"
                    }, {
                        firstName: "Peter",
                        lastName: "Jones"
                    }

我用正则表达式编写了下面的代码来遍历数组:

var selector = {
    chatUsers: [{
        firstName: regExp
    }, {
        lastName: regExp
    }]
};
console.log("UA" + UT.find(selector, options).fetch())

但它没有返回任何东西。建议我这样做。

这是我的代码:

if (searchText) {
    console.log("searchText")
    var regExp = buildRegExp(searchText);
    var selector = {
        chatUsers: [{
            firstName: regExp
        }, {
            lastName: regExp
        }]
    };
    console.log("UA" + UT.find(selector, options).fetch())

    return UT.find(selector, options).fetch();

} else {
    return UT.find({}, options).fetch();
}
});

这是我写表达式的地方:

function buildRegExp(searchText) {
    // this is a dumb implementation
    var parts = searchText.trim().split(/[ \-\:]+/);
    return new RegExp("(" + parts.join('|') + ")", "ig");
}

【问题讨论】:

  • 尝试regexr.com 对您的正则表达式进行故障排除,然后再用 javascript 处理它们。

标签: javascript meteor


【解决方案1】:

一切正常

var regExp = buildRegExp(searchText);
console.log("searchText :" + regExp)
var selector = {chatUsers: { $all: [
{ "$elemMatch" : { firstName: {$regex:regExp}, {$regex:regExp} }   },                                   
                              ]
              }}
console.log("serach Result ::"+UT.find(selector).fetch())
//This where I have wrote the expression:
function buildRegExp(searchText) {

return new RegExp("^"+searchText,"ig")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2013-06-14
    相关资源
    最近更新 更多