1

出于某种奇怪的原因,iron-router随机返回未定义。

this.route('pollyShow', {
    path: '/polly/:_id',
    template: 'polly_show',
    notFoundTemplate: 'notFound',
    before: function () {
        var id = this.params._id;
        var poll = Polls.findOne({_id: id});
        console.log(poll);
        var ip_array = poll.already_voted;
        $.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
            ip_voted = ip_array.indexOf(data.host);

            if (ip_voted > -1) {
                Router.go('pollyResults', {_id: id});
            }
        });
    },
    data: function() {

        return Polls.findOne({_id: this.params._id});
    }
});

有时它正常返回,而其他时候它只是返回未定义。

这背后有什么原因吗?

4

1 回答 1

0

出现此问题的原因Polly是,当路由执行时,集合有时会被填充,而其他时候则不会被填充。

可以通过在路由配置中显式等待subscriptionusing选项来防止此问题。waitOn


文档

默认情况下,一个新的 Meteor 应用程序包括 autopublish 和 insecure 包,它们共同模仿每个客户端对服务器数据库具有完全读/写访问权限的效果。这些是有用的原型设计工具,但通常不适合生产应用程序。准备好后,只需卸下包裹即可。

要删除软件包,请调用meteor remove <package-name>

然后您需要在服务器上显式发布您希望在客户端上看到的记录:

服务器/publications.js

Meteor.publish('all_of_polly', function () { return Polls.find({}); });

并在客户端订阅它:

this.route('pollyShow', {
    path: '/polly/:_id',
    template: 'polly_show',
    notFoundTemplate: 'notFound',
    waitOn: function () { return Meteor.subscribe('all_of_polly'); }
    // ...
});
于 2014-02-20T18:09:42.253 回答