流星铁路由器没有将MongoDB集合数据传递给模板

时间:2015-02-11 07:29:44

标签: meteor iron-router

我使用Iron Router程序包创建了一个基本的Meteor应用程序。用一些数据在MongoDB数据库中播种,并试图告诉Iron Router将该集合提供给模板。但数据不会进入模板。

/collections.js

Wip = new Mongo.Collection('wip');

/app.js

Router.configure({
  layoutTemplate: 'masterLayout'
});
Router.route('wip2', {
  path: '/wip2/:shortname',
  template: 'wip2',
  data: function() {
    return Wip.findOne( { shortname: this.params.shortname } );
  }
});

在那里你可以看到集合查询。在wip2.html文件中,我有{{name}}和{{shortname}}等变量,但它们都没有显示任何内容。我通过在服务器上执行“meteor mongo”并运行查找来确认本地MongoDB确实有数据:

db.wip.findOne( {shortname : 'JonSmith'} );

MongoDB确实返回数据,如下所示:

{
  "shortname" : "JonSmith",
  "name" : "Jon Smith"
}

为了确认Iron Router实际上可以将数据传递到模板中,我可以对数据进行硬编码,从而成功地将数据传递到模板中:

/app.js

Router.route('wip2', {
  path: '/wip2/:shortname',
  template: 'wip2',
  data: {
    shortname: "JonSmith",
    name:   "John Smith"
  }
});

有效。 wip2.html中的{{shortname}}和{{name}}会提供值。

有关为什么没有将收集结果提供给模板的任何想法?提前谢谢!

1 个答案:

答案 0 :(得分:0)

你可以尝试这段代码,

Router.route('wip2', {
  path: '/wip2/:shortname',
  template: 'wip2',
  data: function() {
    templateData = {
        Wip : Wip.find( { shortname: this.params.shortname },{limit : 1} );
     };
  return templateData;
  }
});

findOne 只返回一个对象,而 find limit = 1 将返回光标,无论如何都可以使用。

相关问题