我有个问题。我正在努力建立高图表图形。工作原理:我要按我的路线走("小细节"(,在这里我没有问题。我的问题:对(ships_snapshots_all(的订阅不起作用。我的出版物js:
Meteor.publish("ships_snapshots", function(user, options) {
if(!this.userId) return null;
if(this.userId) {
console.log('subsribed by ' + user);
return ships_snapshots.find({userId: user}, options);
}
});
Meteor.publish("ships_snapshots_all", function() {
return ships_snapshots.find({});
})
我的subscribe.js(在lib文件夹中(:
Meteor.subscribe('ships_snapshots');
Meteor.subscribe('ships_snapshots_all');
问题100%在我的订阅,因为如果我安装自动发布所有工作良好。我觉得我的路由器有问题。
router.js:
Router.route('/ships/details', {
name: 'ship.details',
loadingTemplate: 'loading',
onBeforeAction: function() {
var shipId = Session.get('currentShipId');
if(!shipId) {
Router.go('user.ships');
} else {
this.next();
}
},
waitOn: function() {
if (Meteor.isClient) {
var getCompare = Meteor.user().profile.wows.compareWith;
console.log(getCompare);
var user2 = Meteor.users.findOne({"profile.wows.nickname": getCompare});
var user2Id = user2._id;
if (getCompare) {
var user2 = Meteor.users.findOne({"profile.wows.nickname": getCompare});
if (user2) {
var user2Id = user2._id;
}
}
if (getCompare) {
var handle = Meteor.subscribe('ships_snapshots', Meteor.user()._id) && Meteor.subscribe('ships_snapshots', user2Id) && Meteor.subscribe('userSearchInfo', getCompare);
Session.set('compareWith', user2);
console.log('user2 _____');
console.log(user2);
return handle
} else {
var handle = Meteor.subscribe('ships_snapshots', Meteor.user()._id) && Meteor.subscribe('ships_snapshots', user2Id);
return handle
}
}, data: function() {
if (handle.ready()) {
var shipname = this.params.shipName;
var obj = {};
var query = ships.findOne();
var shipId = Session.get('currentShipId');
var result;
_.each(Meteor.user().profile.wows.ships, function(row) {
if (row.ship_id === shipId) {
result = row;
}
});
return result;
}
}
});
我认为我在订阅ship_snapshots时遇到了问题。这里出了问题,但我解决不了这个问题。
"不工作"到底是什么意思?根据你的代码,我假设你总是看到所有的飞船快照。
如果你的路由器中有订阅,你就不应该在/lib中有订阅。如果你在/lib中有Meteor.subscribe('ships_snapshots_all');
,那么你应该总是看到所有的发货快照(假设你没有在任何地方停止订阅(。
此外,您对所有内容的订阅应为:
Meteor.publish("ships_snapshots", function(user, options) {
if(this.userId) {
console.log('subsribed by ' + user);
return ships_snapshots.find({userId: user}, options);
} else this.ready();
});
当没有用户时,您不想返回null,您可以在没有找到任何记录的情况下将订阅标记为就绪。这不是你问题的原因,只是一个好的做法。
Meteor.publish("ships_snapshots", function(user, options) {
if(!this.userId) return null;
if(this.userId) {
console.log('subsribed by ' + user);
return ships_snapshots.find({userId: user._id}, options);
}
});
在您的发布脚本中,user真的是id还是用户对象?我把它改成了user_id。请检查一下。