Simple search function in meteor using EasySearch package

只谈情不闲聊 提交于 2019-12-25 06:41:07

问题


Good Day,

I'm trying to create a simple search function using the easy search package.

In a nutshell I have done the following-

Defined a schema and index on the client as such:

const Patients = new Mongo.Collection('patients');

const PatientsIndex = new EasySearch.Index({
    collection: Patients,
    fields: ['name'],
    engine: new EasySearch.MongoDB()
  });

I've entered the following values into the database:

meteor:PRIMARY> db.patients.find()
{ "_id" : ObjectId("57d6a9f71bad26ba07748f9d"), "name" : "Paul" }

Created a template helper on the client side:

Template.searchBox.helpers({
  patientsIndex: () => PatientsIndex
});

And lastly I've created a template which should output the results:

<template name="searchBox">
    {{> EasySearch.Input index=patientsIndex }}
    <ul>
        {{#EasySearch.Each index=patientsIndex }}
            <li>Name of the patient: {{name}}</li>
        {{/EasySearch.Each}}
    </ul>
</template>

Now for some reason this just wont work, it renders nothing to the template, I' very new to this and would really appreciate some assistance.

Thanking you.


回答1:


From your code samples it looks like you're trying to refer to both Patients and PatientsIndex globally. Assuming you have your Patients and PatientsIndex declarations in a shared client/server location (like /lib), then you should remove the const keyword. That will make sure these declarations are available globally, and will allow your Template to use them. Here's a modified version of your code that will work:

/lib/collection.js

Patients = new Mongo.Collection('patients');

PatientsIndex = new EasySearch.Index({
  collection: Patients,
  fields: ['name'],
  engine: new EasySearch.MongoDB()
});

/client/main.html

<body>
  {{> searchBox}}
</body>

<template name="searchBox">
  {{> EasySearch.Input index=patientsIndex }}
  <ul>
    {{#EasySearch.Each index=patientsIndex }}
      <li>Name of the patient: {{name}}</li>
    {{/EasySearch.Each}}
  </ul>
</template>

/client/main.js

import { Template } from 'meteor/templating';
import './main.html';

Template.searchBox.helpers({
  patientsIndex() {
    return PatientsIndex;
  }
});


来源:https://stackoverflow.com/questions/39451379/simple-search-function-in-meteor-using-easysearch-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!