meteorhacks / search-source

Reactive Data Source for Search
MIT License
146 stars 33 forks source link

Exception while invoking method 'search.source' Error: Match error: Failed Match.OneOf or Match.Optional validation #47

Open achirkof opened 8 years ago

achirkof commented 8 years ago

Hello. I'm trying to implement this package but have an error:

Exception while invoking method 'search.source' Error: Match error: Failed Match.OneOf or Match.Optional validation
    at check (packages/check/match.js:33:1)
    at [object Object]._.extend._getFindOptions (packages/mongo/collection.js:248:1)
    at [object Object]._.extend.find (packages/mongo/collection.js:284:1)
    at [object Object].<anonymous> (server/search-source.js:7:1)
    at [object Object].getSourceData (packages/meteorhacks_search-source/lib/server.js:65:1)
    at [object Object].Meteor.methods.search.source (packages/meteorhacks_search-source/lib/server.js:18:1)
    at maybeAuditArgumentChecks (livedata_server.js:1698:12)
    at livedata_server.js:708:19
    at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
    at livedata_server.js:706:40
Sanitized and reported to the client as: Match failed [400]

Here is my code. Client

var options = {
    keepHistory: 1000 * 60 * 5,
    localSearch: true
};
var fields = ['exerciseName', 'exerciseDesc'];

ExercisesSearch = new SearchSource('exercises', fields, options);

Server

SearchSource.defineSource('exercises', function(searchText, options) {
    //var options = {sort: {isoScore: -1}, limit: 20};

    if(searchText) {
        var regExp = buildRegExp(searchText);
        var selector = {exerciseName: regExp, exerciseDesc: regExp};
        return Exercises.find(selector, options).fetch();
    } else {
        return Exercises.find({}, options).fetch();
    }
});

function buildRegExp(searchText) {
    var words = searchText.trim().split(/[ \-\:]+/);
    var exps = _.map(words, function(word) {
        return "(?=.*" + word + ")";
    });
    var fullExp = exps.join('') + ".+";
    return new RegExp(fullExp, "i");
}
achirkof commented 8 years ago

The problem is in options parameter here:

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

I don't really know why, but when I remove it everything working fine.

vladejs commented 8 years ago

you are passing on options variable a property that hasn't the expected type.

It happened to me when i was passing a limit parameter within my helper method of type string instead of an integer.

Put a console.log(options,selector) on your server method to identify what variable has it's wrong data type. In my case the query looked like:

cursor = Collection.find(selector, {limit : '' })

where limit should be an integer.