我试图在每个用户拥有的数组中发布由用户名创建的帖子。我将这个添加到显微镜实习应用中,错误是I20140826-20:31:53.452(-4)?在Meteor.publish.Comments.find.postId 处理程序。谢谢,这是代码。
publications.js循环应该在下面的数组中发布由每个用户名生成的帖子。
Meteor.publish('posts', function(options) {
for (u=0;u<this.user.profile.following.length;u++) {
f=profile.following[u].text();
return Posts.find({username:f}, options);
}
});
它会影响到的路线
控制器
PostsListController = RouteController.extend({
template: 'postsList',
increment: 5,
limit: function() {
return parseInt(this.params.postsLimit) || this.increment;
},
findOptions: function() {
return {sort: this.sort, limit: this.limit()};
},
waitOn: function() {
return Meteor.subscribe('posts', this.findOptions());
},
posts: function() {
return Posts.find({}, this.findOptions());
},
data: function() {
var hasMore = this.posts().count() === this.limit();
return {
posts: this.posts(),
nextPath: hasMore ? this.nextPath() : null
};
}
});
NewPostsListController = PostsListController.extend({
sort: {submitted: -1, _id: -1},
nextPath: function() {
return Router.routes.newPosts.path({postsLimit: this.limit() + this.increment})
}
});
BestPostsListController = PostsListController.extend({
sort: {votes: -1, submitted: -1, _id: -1},
nextPath: function() {
return Router.routes.bestPosts.path({postsLimit: this.limit() + this.increment})
}
});
路由器映射
this.route('newPosts', {
path: '/new/:postsLimit?',
controller: NewPostsListController
});
this.route('bestPosts', {
path: '/best/:postsLimit?',
controller: BestPostsListController
});
发布于 2014-08-26 18:25:01
您的发布函数是错误的,您是否意识到您的循环是无用的,因为它在第一次返回时就退出了?
即使您是在聚合循环中累积的游标,这也是行不通的,因为发布函数目前只能从不同的集合返回多个游标。
您需要在这里使用适当的mongo选择器,可能是$in
。
而且,在发布函数中甚至没有定义profile.following
,迭代数组的方法是根据数组长度(profile.following.length
)检查迭代器变量,或者更好地使用Array.prototype.forEach
或_.each
。
我想这就是你想要做的
Meteor.publish("posts",function(options){
if(!this.userId){
this.ready();
return;
}
var currentUser=Meteor.users.findOne(this.userId);
return Posts.find({
username:{
$in:currentUser.profile.following
}
},options);
});
在深入研究Meteor之前,您绝对应该阅读有关JavaScript本身的资源,如果您正在阅读Discover Meteor
的书,我认为它们为初学者提供了一些很好的JS教程。
https://stackoverflow.com/questions/25517323
复制