matteodem / meteor-easy-search

Easy-to-use search for Meteor with Blaze Components
MIT License
438 stars 68 forks source link

Match error: Expected particular constructor #535

Closed damienrobson closed 7 years ago

damienrobson commented 7 years ago

The following code throws the titular error when I try to run my application:

const MembersIndex = new EasySearch.Index({ ... });
export default MembersIndex;

Any ideas as to how I can resolve this? Full error below:

Error: Match error: Expected particular constructor
    at exports.check (packages/check/match.js:34:1)
    at new Index (packages/easysearch:core/lib/core/index.js:17:5)
    at meteorInstall.imports.api.members.search.js (imports/api/members/search.js:4:22)
    at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
    at Module.require (packages/modules-runtime/.npm/package/node_modules/install/install.js:82:1)
    at Module.Mp.import (/home/skulmuk/.meteor/packages/modules/.0.7.6.msbdsb++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
    at meteorInstall.imports.startup.server.register-api.js (imports/startup/server/register-api.js:1:1)
    at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
    at Module.require (packages/modules-runtime/.npm/package/node_modules/install/install.js:82:1)
    at Module.Mp.import (/home/skulmuk/.meteor/packages/modules/.0.7.6.msbdsb++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
Exited with code: 1
Your application is crashing. Waiting for file change.

Or does anyone know how to stop: exports.check (packages/check/match.js:34:1)

matteodem commented 7 years ago

Can you show me what code you have inside the ...?

damienrobson commented 7 years ago

Gist

matteodem commented 7 years ago

what kind of a collection is Members? Is it a MongoDB.Collection?

damienrobson commented 7 years ago
export const Members = new Mongo.Collection('members');
AdrianAbba commented 7 years ago

getting the same error, any solution found?

matteodem commented 7 years ago

Can you show me which packages you are using in your project?

AdrianAbba commented 7 years ago

fixed it but geeting this error now :x

errorClass details : undefined error : "no-index" errorType : "Meteor.Error" message : "Please provide an index for your component [no-index]" reason : "Please provide an index for your component"

......

AdrianAbba commented 7 years ago

fixed :)

matteodem commented 7 years ago

what was the "fix"?

damienrobson commented 7 years ago

I've now fixed this issue - it was to do with the import process of using export const and then importing with import {Members} .... Still might be worth increasing the verbosity of the error, though :)

damienrobson commented 7 years ago

@matteodem Can you re-open this, please? I'm working on a different project and have the same issue, though I think I understand the cause. Both projects use this package, and particularly, use Meteor.Collection as opposed to Mongo.Collection that you're checking for when creating an index. Is it a simple case of an addition to your check statement to include Meteor.Collection, too?

I've tried to fork it but am hitting brick walls trying to change it myself and see if it solves the issue.

matteodem commented 7 years ago

What keeps you from using Mongo.Collection? is the Meteor.Collection prototype being enhanced by that package?

If that's the case I'd propose that you fork the package and adjust the code, because Meteor.Collection should not be used anymore.

damienrobson commented 7 years ago

I tried forking the other package and changing it to Mongo.Collection, but I still get the error when trying to initialise an EasySearch with the collection propert set to the package's collection name.

matteodem commented 7 years ago

What version of meteorjs are you using?

damienrobson commented 7 years ago

It's OK - we've found a workaround. The check only fails in production mode, so we've done the following:

import { EasySearch } from 'meteor/easy:search';
import { Meteor } from 'meteor/meteor';

const Tags = Meteor.tags;

Tags.index = new EasySearch.Index({
  collection: new Mongo.Collection(null),
  fields: ['name'],
  engine: new EasySearch.MongoDB({
    sort: () => ({ name: 1 }),
    fields: (searchObject, options) => {
      if (options.search.props.autosuggest) {
        return {
          name: 1,
          nRefs: 1,
        };
      }
      return {};
    },
    selector(searchObject, options, aggregation) {
      const selector = this.defaultConfiguration().selector(searchObject, options, aggregation);
      if (options.search.props.collection) {
        selector.collection = options.search.props.collection;
      }
      if (options.search.props.searchById) {
        selector._id = options.search.props.searchById;
      }
      return selector;
    },
  }),
});

// Easysearch checks collection is a Mongo.Collection, and this fails in production mode
// (but not in dev mode!). So we set the collection null above and assign here as the check only
// occurs in the constructor. Not great, but gets around the issue...
Tags.index.config.collection = Meteor.tags;
Tags.index.config.name = Meteor.tags._name;

export default Tags;