【问题标题】:Meteor: Reactive Join with "publish-with-relations"-packageMeteor:使用“publish-with-relations”-package 的反应式加入
【发布时间】:2014-01-02 04:33:05
【问题描述】:

我的 Meteor 项目中有以下数据结构:
- 具有一组属于用户(作者)的列表 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;
    }
});

}

发布不起作用,并且光标不包含启动的 List 元素。

知道如何解决此反应式联接以发布某个用户的列表吗?提前致谢,感谢您的帮助!

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    您的测试数据未正确插入。再看看我对您的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
        }]
      });
    });
    

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

    【讨论】:

      猜你喜欢
      • 2014-03-20
      • 2015-01-22
      • 2013-11-29
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 2018-05-23
      相关资源
      最近更新 更多