Meteor:Reactive Join with" publish-with-relations" -package

时间:2014-01-01 17:55:45

标签: javascript mongodb meteor

我的Meteor项目中有以下数据结构:
- 具有属于用户(作者)的一组list-id的用户
- 实际包含列表中所有数据的列表以及允许查看它的一组用户ID(以及所有者)

现在,我正在尝试使用 publish-with-relations-package Toms version from GitHub)将用户的所有列表发布到客户端。这是一个简单的例子:

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

if (Meteor.isClient) {

    Deps.autorun(function() {
        if (Meteor.userId()) {
            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) {

    Meteor.startup(function () {
        if ( Meteor.users.find().count() === 0 ) {
               var user = 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 listid = new Meteor.Collection.ObjectID().valueOf();
               Meteor.users.update(user._id, {$addToSet : {lists : listid}});
               Lists.insert({_id : listid, data : 'content', owner : user._id});
        }
     });


Meteor.publish('lists', function(id) {

    Meteor.publishWithRelations({
        handle: this,
        collection: Lists,
        filter: _id,
        mappings: [{
            key: 'lists',
            collection: Meteor.users
        }]
    });
});

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


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

}

发布不起作用,Cursor不包含已启动的List元素。

知道如何修复此反应式联接以发布某个用户的列表吗?在此先感谢,任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:4)

您的测试数据未正确插入。再看看我的previous question解决方案。关键区别在于Accounts.createUser会返回 id ,而不是对象。

无论如何,PWR有点令人困惑,但我认为正确的方法是发布用户,然后发布Lists作为关系。即使每个列表文档似乎都有一个所有者,我假设目标是在用户的lists数组中发布所有列表文档。

Meteor.publish('lists', function() {
  Meteor.publishWithRelations({
    handle: this,
    collection: Meteor.users,
    filter: this.userId,
    mappings: [{
      key: 'lists',
      collection: Lists
    }]
  });
});

尝试一下,如果遇到问题请告诉我。