在Meteor中搜索一个集合

时间:2016-03-23 00:59:23

标签: javascript mongodb meteor

我正在尝试创建一个小规模的流星应用,以便在mongodb集合中搜索我已经添加到其中的任何数据。

我现在显示整个集合,但是如果我在表单中输入搜索字符串,我希望它只显示包含相同搜索字符串的mongodb条目。这是我到目前为止所得到的。

app.js:

Persons = new Mongo.Collection("persons");

if (Meteor.isClient) {
  Template.body.helpers({
  persons: function() {
    return Persons.find({}, {sort: {createdAt: -1}});
  }
});

Template.person.persons({
  "submit .person-search": function(event) {
    event.preventDefault();

    var input = event.target.text.value;

    return Persons.find({
      text: input
    });
  }
});

app.html中的搜索表单

<form class = "person-search">
  <input type = "text" name = "text" placeholder = "Type to search for people" />
</form>

app.html中的集合显示

<ul>
  {{#each persons}}
    {{> person}}
  {{/each}}
</ul>

每当我输入搜索字符串时,集合将刷新,但会继续显示集合中的所有数据。我尝试过使用像这样的会话,我从这里开始:How to Search and output from Meteor Collection

Template.person.persons = function() {
  return Persons.find({
  person: { $regex: Session.get('prefix') + ".*", $options: 'i'}});
};

Template.person.events({
  "submit .person-search": function() {
  Session.set('prefix', $('.person-search').val());
  }
});

然而,这似乎并不适合我。如果有人能指出我正确的方向,将不胜感激。谢谢大家的时间。

0 个答案:

没有答案
相关问题