流星铁路由器:在路由之间传递数据

Fre*_*cer 12 javascript handlebars.js meteor iron-router

如何在两个不同的路由和模板之间传递数据?

我在前端(客户端文件夹)上有一个javascript文件,它只调用Router.go()作为我的参数之一传入帖子ID.

以下是三个主要罪魁祸首(我相信).我删除了大部分代码,以便于阅读.我可以毫无问题地更改到PostDetail页面.我还可以检索帖子IDPostDetail从路由器页面.我的问题是,检索到的数据库条目(POLL)不会在模板上呈现.因此,即使返回数据库条目,{{Question}}也始终为空.

如果我发布更多信息,请告诉我.

FrontEnd.js

Template.PostTiles.events({
  // When a choice is selected
  'click .pin' : function(event, template) {        
    Router.go('Post', {_PostId: this.PostId});    
  }
});
Run Code Online (Sandbox Code Playgroud)

后detail.html

<template name="PostDetail">
    <h3>{{Question}}</p>
</template>
Run Code Online (Sandbox Code Playgroud)

Shared.js

Router.map( function() {

    this.route('Home', {
        path: '/',
        template: 'PostTiles',
        data: {
            // Here we can return DB data instead of attaching 
            // a helper method to the Template object
            QuestionsList: function() {
                return POLL.find().fetch();
            }           
        }
    });

    this.route('Post', {
        template: 'PostDetail',
        path: '/Post/:_PostId',
        data: function() {          
            return POLL.findOne(this.params._PostId); 
        },
        renderTemplates: {
            'disqus': {to: 'comments'}
        }
    });

});
Run Code Online (Sandbox Code Playgroud)

-----更新-----

我想我已经将问题缩小到只能呈现一个数据库条目,而不是使用{{#each SomeList}}语法来呈现它们的列表.

小智 2

看起来你找到了答案/解决了这个问题,但为了以防万一,我认为它在你的findOne声明中:

data: function() {          
        return POLL.findOne(this.params._PostId); 
    },
Run Code Online (Sandbox Code Playgroud)

应该读:

data: function() {          
        return POLL.findOne({_id:this.params._PostId}); 
    },
Run Code Online (Sandbox Code Playgroud)

(假设 POLL 按 _id 列出了您的帖子。

希望有帮助。