Javascript 《流星》中未定义的铁路由器
出于某种奇怪的原因,Javascript 《流星》中未定义的铁路由器,javascript,meteor,iron-router,Javascript,Meteor,Iron Router,出于某种奇怪的原因,iron router随机返回undefined this.route('pollyShow', { path: '/polly/:_id', template: 'polly_show', notFoundTemplate: 'notFound', before: function () { var id = this.params._id; var poll = Polls.findOne({_id: id}
iron router
随机返回undefined
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});
}
});
有时它正常返回,而有时它只是返回未定义
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});
}
});
这背后有什么原因吗 出现此问题的原因是执行路由时,
Polly
集合有时会被填充,有时则不被填充
通过在路由配置中明确使用waitOn
选项,可以防止此问题
从: 默认情况下,一个新的Meteor应用程序包括自动发布和不安全包,它们共同模拟每个客户端对服务器数据库具有完全读/写访问权限的效果。这些是有用的原型工具,但通常不适用于生产应用程序。准备好后,只需删除包 要删除软件包,请调用
meteor remove
然后,您需要在服务器上的客户端上显式发布要查看的记录:
服务器/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'); }
// ...
});
恐怕我不明白:这里返回的
undefined
是什么?我的意思是poll
是控制台中的undefined
。log(poll);这就是使用console.log在data:function(){..}
下返回的内容。我可以看到,由于某种原因,它实际上是未定义的。这可能是因为来自订阅的数据尚未填充客户端的Polly
集合。对Polly
的订阅设置wait
,以确保在处理路由之前它已准备就绪。我没有设置任何订阅。我如何设置等待?由于某种原因,这对我不起作用,导致控制台中出现错误<代码>未捕获类型错误:未定义不是一个函数以下是我在代码中得到的内容:您在路由加载后定义集合?它们应该在一个单独的文件中,在客户端和服务器上运行。另外,了解什么是未定义的
。route.js
文件既不在客户机目录中,也不在服务器目录中。它位于应用程序的主目录中。至于说什么是未定义的是这一行Polls=newmeteor.collection('Polls_all')代码>不确定出了什么问题。我在Meteor.subscribe('polls_all')上面有一行代码代码>错误似乎是Meteor.collection()
的大小写应该是Meteor.collection())
修复了一个问题,但是新的错误是Deps重新计算的异常:错误:已经有一个名为“polls\u all”的集合
这可能是因为我在waitOn和before方法中有Meteor.subscribe,但是,我不知道如何以任何其他方式访问该集合。集合本身也不正确。我只有一个meteor.collection对象。好吧,我明白,自动发布是为了原型制作(为了方便)。。。但是,难道没有一种方法可以在有自动发布和没有自动发布的情况下工作吗?