Javascript 为什么这会导致服务器错误

Javascript 为什么这会导致服务器错误,javascript,meteor,iron-router,Javascript,Meteor,Iron Router,我试图在每个用户拥有的以下数组中发布由用户名创建的帖子。我将此添加到错误中,错误是I20140826-20:31:53.452(-4)?在Meteor.publish.Comments.find.postId[as_handler](app/server/publications.js:2:13)。高级谢谢这里是代码 publications.js 循环应该发布以下数组中每个用户名的帖子 Meteor.publish('posts', function(options) { for (u=0;u

我试图在每个用户拥有的以下数组中发布由用户名创建的帖子。我将此添加到错误中,错误是I20140826-20:31:53.452(-4)?在Meteor.publish.Comments.find.postId[as_handler](app/server/publications.js:2:13)。高级谢谢这里是代码

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);
  }
});
路由器地图

  this.route('newPosts', {
    path: '/new/:postsLimit?',
    controller: NewPostsListController
  });

  this.route('bestPosts', {
    path: '/best/:postsLimit?',
    controller: BestPostsListController
  });

您的发布函数是错误的,您是否知道您的循环是无用的,因为它在遇到第一个返回时退出

即使聚合循环中累积的游标,这也不起作用,因为此时发布函数只能返回来自不同集合的多个游标

您需要在此处使用适当的mongo选择器,它可能是
$in

另外,
profile.following
甚至没有在publish函数中定义,通过对照数组长度检查迭代器变量(
profile.following.length
),或者更好的方法是使用
array.prototype.forEach
来完成数组的迭代

我想这就是你想要做的:

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本身的参考资料,如果您正在阅读
探索Meteor
这本书,我认为他们为初学者提供了一些很好的JS教程。

您需要解决这个问题,对于初学者来说,让它成为一个问题,给出错误,也是循环的第一步?你期望它做什么?我认为问题来自于调用
profile上的
text
方法。following[u]
:没有这样的方法,
profile。following[u]
可能是一个
字符串
,因此您只需引用它就可以得到它的值。好的,我删除了它,没有任何更改。谢谢您的尝试,但您的代码返回相同的错误集。还有
Meteor.user()不能在发布函数中使用。哦,忘了这一点,我刚刚编辑了我的代码。我把
$的语法也弄糟了,现在应该没问题了。
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);
});