Meteor:Reactive Join with“publish-with-relations”-package [英] Meteor: Reactive Join with "publish-with-relations"-package

查看:67
本文介绍了Meteor:Reactive Join with“publish-with-relations”-package的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Meteor项目中有以下数据结构:

- 具有属于用户(作者)的一组list-id的用户

- 列出实际上包含列表的所有数据和一组允许查看它的用户ID(以及所有者)

I'm having the following data structure in my Meteor project:
- Users with a set of list-ids that belong to the user (author)
- Lists that actually contain all the data of the list and a set of user-ids that are allowed to view it (and a owner)

现在我正在尝试发布所有列表使用 publish-with-relations-package GitHub的Toms版本。这是一个简单的例子:

Now I'm trying to publish all Lists of a user to the client with the publish-with-relations-package (Toms version from GitHub). Here is a simple example:

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不包含已启动的列表元素。

Publishing is not working and the Cursor doesn't contain the List element that is initiated.

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

Any idea how to fix this reactive join for publishing Lists of a certain user? Thanks in advance, any help is appreciated!

推荐答案

您的测试数据未正确插入。再看看我的上一个问题。关键区别在于 Accounts.createUser 返回 id ,而不是对象。

Your test data isn't being inserted correctly. Have another look at my solution to your previous question. The key difference is that Accounts.createUser returns an id, not an object.

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

Anyway, PWR is a bit confusing but I think the correct approach is to publish the user, and then publish Lists as the relation. Even though each list document seems to have an owner, I'm assuming the goal is to publish all list documents in the user's lists array.

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

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

Give that a try and let me know if you run into problems.

这篇关于Meteor:Reactive Join with“publish-with-relations”-package的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆