我目前正在创建一个流星应用程序使用铁路由器和有困难返回2个数据上下文。
Router.map(function() {
this.route('account', {
path: '/account/:accountId',
template: 'account',
data: function(){
//assigning the data object to self in order to use the 'accountId' route paramter
var self = this;
var currentRuns = function(){
return CmrRuns.find({accountId: self.params.accountId});
}
var currerntAccount = function(){
return CmrAccounts.findOne({accountId: self.params.accountId});
}
var current = {runs: currentRuns, account: currentAccount};
return current;
}
});
我要做的是从cmraccounts集合获取帐户信息,从cmrruns集合获取运行信息。
我当前收到“currentAccount未定义”错误。
我已经被困在这上面两天了,不知所措。
你知道我在这里做错了什么吗?或者在使用Iron Router路由时,有没有其他方法可以获取两组数据?
最佳答案
首先检查你的订阅。你能在控制台中运行同样的客户端查询并得到你想要的结果吗?
其次,我将简化数据方法。这应该有效:
data: {
runs: CmrRuns.find({accountId: this.params.accountId}),
account: CmrAccounts.findOne({accountId: this.params.accountId})
}
不过,请注意,
.map
已弃用:https://github.com/iron-meteor/iron-router/issues/1503这可能是许多问题的根源。我还要高度赞扬放弃这种方法。另一种方法是查看FlowRouter。通过使用模板处理订阅和数据,它变得更整洁、更易于诊断、维护和迁移。此外,维护路由之间的数据状态要容易得多,在构建复杂的ui时可能会遇到这种情况。