我的Meteor项目中具有以下数据结构:
-具有一组属于该用户(作者)的列表ID的用户
-实际包含列表中所有数据的列表

现在,我正在尝试将用户的所有列表发布到客户端。这是一个简单的示例:

   

if (Meteor.isClient) {
    Lists = new Meteor.Collection("lists");

    Deps.autorun(function() {
        Meteor.subscribe("lists");
    });

  Template.hello.greeting = function () {
    return "Test";
  };

  Template.hello.events({
    'click input' : function () {
      if (typeof console !== 'undefined')
        console.log(Lists.find());
    }
  });
}

if (Meteor.isServer) {
    Lists = new Meteor.Collection("lists");

    Meteor.startup(function () {
        if ( Meteor.users.find().count() === 0 ) {
               Accounts.createUser({        //create new user
                   username: 'test',
                   email: 'test@test.com',
                   password: 'test'
               });

               //add list to Lists and id of the list to user
               var user = Meteor.users.findOne({'emails.address' : 'test@test.com', username : 'test'});
               var listid = new Meteor.Collection.ObjectID().valueOf();
               Meteor.users.update(user._id, {$addToSet : {lists : listid}});
               Lists.insert({_id : listid, data : 'content'});
        }
     });


    Meteor.publish("lists", function(){
        var UserListIdsCursor = Meteor.users.find({_id: this.userId}, {limit: 1}).lists;
        if(UserListIdsCursor!=undefined){
            var UserListIds = UserListIdsCursor.fetch();

            return Lists.find({_id : { $in : UserListIds}});
        }
    });

    Meteor.publish("mylists", function(){
        return Meteor.users.find({_id: this.userId}, {limit: 1}).lists;
    });


//at the moment everything is allowed
Lists.allow({
    insert : function(userID)
    {
        return true;
    },
    update : function(userID)
    {
        return true;
    },
    remove : function(userID)
    {
        return true;
    }
});

}


但是发布列表无法正常工作。任何想法如何解决这一问题?我还将发布“我的列表”,以确保用户可以访问“列表”字段。

最佳答案



Lists = new Meteor.Collection('lists');

if (Meteor.isClient) {
  Tracker.autorun(function() {
    if (Meteor.userId()) {
      Meteor.subscribe('lists');
      Meteor.subscribe('myLists');
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function() {
    if (Meteor.users.find().count() === 0) {
      var user = {
        username: 'test',
        email: 'test@test.com',
        password: 'test'
      };

      var userId = Accounts.createUser(user);
      var listId = Lists.insert({data: 'content'});
      Meteor.users.update(userId, {
        $addToSet: {lists: listId}
      });
    }
  });

  Meteor.publish('lists', function() {
    check(this.userId, String);
    var lists = Meteor.users.findOne(this.userId).lists;
    return Lists.find({_id: {$in: lists}});
  });

  Meteor.publish('myLists', function() {
    check(this.userId, String);
    return Meteor.users.find(this.userId, {fields: {lists: 1}});
  });
}


变化


在客户端和服务器外部声明Lists集合(无需声明两次)。
订阅时,请确保用户已登录。 (性能增强)。
插入测试用户时,请使用以下事实:所有插入函数都返回一个ID(减少代码)。
确保发布时用户已登录。
简化的lists发布功能。
修复了myLists发布功能。发布需要返回游标,游标数组或伪造的值。您无法返回ID数组(由于需要执行fetchfindOne,所以该代码也无法访问)。重要说明-这将发布另一个具有lists字段的用户文档。在客户端上,它将与现有用户文档合并,因此只有登录用户具有lists。如果希望所有用户在客户端上都具有该字段,那么建议您仅将其添加到用户配置文件中。


警告:撰写本文时,如果附加了其他列表项,则不会发布它们,因为lists发布功能仅在用户登录时才重新运行。要使其正常工作,您将需要一个reactive join

关于javascript - meteor :如何发布取决于其他集合的游标的游标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20825758/

10-16 22:00