从搜索中检索显示结果



我对流星比较陌生.js我正在尝试让搜索表单工作。到目前为止,我什至没有尝试让参数工作,但它稍后会到来。

我基本上是想让一堆电梯展示。

库/路由器.js

Router.configure({
  layoutTemplate: 'layout',
  loadingTemplate: 'loading',
  notFoundTemplate: 'notFound',
  waitOn: function() {
    return Meteor.subscribe('lifts');
  }
});
Router.route('/', { name: 'liftsList' });
Router.route('/lifts/search/:from-:to-:when', {
  name: 'liftsSearch',
  waitOn: function() {
    return Meteor.subscribe('liftsSearch');
  }
});

服务器/发布.js

Meteor.publish('liftsSearch', function() {
  var query = { fromLoc: { $near : { 
    $geometry: { 
      type : "Point" ,
      coordinates: [ 6.11667, 45.9 ]
    } },
    $maxDistance : 50
  }};
  return Lifts.find(query);
});

如果我尝试使用 Lifts.find(query(.fetch(( 显示结果,它会返回实际结果。

client/lifts_search.html

<template name="liftsSearch">
  <div class="container">
    <h3>Lifts search results {{hi}}</h3>
    <div class="lifts">
      {{#each lifts}}
        hi
        {{> liftItem}}
      {{/each}}
    </div>
  </div>
</template>

在这里,我根本没有显示升降机,甚至没有小"嗨"字符串。

谢谢

除非有你没有包含的代码,否则{{#each lifts}}不会呈现,因为你没有在任何地方定义lifts。 仅仅因为您正在填充Lifts集合,模板不会自动知道lifts引用它(主要是因为这完全是任意的 - 它会引用什么确切的查询?

因此,您需要在路由器data函数中定义lifts

Router.route('/lifts/search/:from-:to-:when', {
  name: 'liftsSearch',
  waitOn: function() {
    return Meteor.subscribe('liftsSearch');
  },
  data: function() {
    return {
      lifts: Lifts.find() // add a query if you want
    }
  }
});

或者在模板帮助程序中:

Template.liftsSearch.helpers({
  lifts: function() {
    return Lifts.find(); // add a query if you want
  }
});

最新更新