如何在Meteor js的路由动作中使用Collection.find()

时间:2016-05-10 15:12:54

标签: meteor iron-router

我有一些代码;

Router.configure({
    layoutTemplate: 'master_layout'
});


Router.map(function(){

    this.route('generatePDF', {
    path: '/pdf',
    where: 'server',
    action: function() {

        console.log(voters.findOne().fetch());
        var voters = voters.find();

    ....
    }
});

如何在路线的操作中使用任何给定的集合。

我得到如下错误;

W20160510-20:19:29.909(5.5)? (STDERR) TypeError: Cannot call method 'findOne' of undefined
W20160510-20:19:29.910(5.5)? (STDERR)     at [object Object].action (lib/routes.js:40:29)
W20160510-20:19:29.910(5.5)? (STDERR)     at boundNext (packages/iron_middleware-stack/lib/middleware_stack.js:251:1)

2 个答案:

答案 0 :(得分:0)

您的代码中有多处错误。

首先,votersundefinedconsole.log(正如您的错误日志所示)。 其次,Voters.findOne()返回单个文档(假设您有一个名为Voters的集合)。您无法在文档上调用fetch。 您可以在光标上调用fetch。例如,Voters.find().fetch()就可以了。

希望有所帮助

答案 1 :(得分:0)

试试这个:

action: function () {
    this.render('someTemplate',{
        data: function(){
            return Voters.find()
        }
    });
}