【问题标题】:Meteor publish-composite and nested collectionMeteor 发布复合和嵌套集合
【发布时间】:2015-08-25 01:31:45
【问题描述】:

我正在尝试构建一个在 Meteor 中具有多对多关系的应用程序。将有工作、客户和用户集合。客户可以有多个工作,最重要的是多个用户可以从事同一个工作。

我在我的fixtures 文件中按如下方式设置了jobs 集合:

Jobs.insert({
  jobNum: 'Somejob',
  clientId: 'XXXXXXXX',
  clientName: 'Some Client',
  rate: XX,
  userNames: [
    {userId: user1._id},
    {userId: user2._id}
  ],
  active: true
});

我正在根据发布复合的自述文件发布,但我无法让用户发布到客户端。这是发布代码:

Meteor.publishComposite('jobsActive', {
  find: function() {
  // Find all active jobs any client
  return Jobs.find({active: true});
  },

  children: [
    {
     find: function (job) {
        // Return a client associated with the job
        return Clients.find({_id: job.clientId}); 
    }
    },
    {
     find: function (job) {
        // Return all users associated with the job
        // This is where the problem is
        return Meteor.users.find({_id: job.userNames.userId});
     }
   }
 ]
});

我不知道如何正确查找数组。我尝试了很多东西,但没有任何效果。这可能吗?还是我需要以其他方式解决这个问题?我考虑过在 users 集合中引用作业,但是作业会比用户多得多,所以这样似乎更有意义。

顺便说一句,我也订阅了“jobsActive”。其他两个集合很好地来到客户端;我只是无法让用户集合发布。

感谢任何帮助和想法。

【问题讨论】:

    标签: javascript mongodb meteor mongodb-query


    【解决方案1】:

    job.userNames.userId 在您的收藏中不存在。 job.userNames 是一个对象数组,其键为 userId

    试试_.map( job.userNames, function( users ){ return users.userId } )

    您的代码将是:

    Meteor.publishComposite('jobsActive', {
        find: function() {
            return Jobs.find({active: true});
        },
        children: [
            {
                find: function (job) {
                    return Clients.find({_id: job.clientId}); 
                }
            },
            {
                find: function (job) {
                    return Meteor.users.find({ _id: { $in: _.map( job.userNames, function( users ) { return users.userId } ) } });
                }
            }
        ]
    });
    

    【讨论】:

    • 这当然更有意义,但它也不起作用。当我在控制台上运行 User.find().count() 时,我仍然得到 0。我确实得到了 Jobs 和 Clients 的正确数字,因此订阅工作正常。我也加载了下划线包。并感谢您的回答。
    • 无法在上面编辑,但我在控制台上运行 Meteor.users.find().count() 并得到 0。
    • 你在运行Jobs.find({}, { fields: { userNames: 1 } })时得到了什么,你能把它贴在这里吗?
    • 我将 .fetch() 添加到您要求的内容中。我得到两个对象。一个有 2 个 userId 数组,一个有 1 个数组。Object _id: "wQ2tGiLTqyRiZrnZN" userNames: Array[2] 0: Object userId: "1" __proto__: Object 1: Object userId: "2" __proto__: Object length: 2 __proto__: Array[0] __proto__: Object Object _id: "xMWHp8NRMCsp5YGYz" userNames: Array[1] 0: Object userId: "2" __proto__: Object length: 1 __proto__: Array[0] __proto__: Object
    • 这些用户ID是现有用户的实际ID吗? userId 的值为“2”,数据库中是否存在 _id = 2 的用户?
    【解决方案2】:

    我认为你根本不需要发布复合,试试这个代码 sn-p。这个对我有用!

    Meteor.publish('jobsActive', function () {
        return Events.find(
        {
            $or: [
                // { public: { $eq: true } },
                { active: true },
                { userNames: this.userId}
            ],
        },
        {
            sort: {createdAt: -1}
        }
        );
    });
    

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2018-03-06
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      相关资源
      最近更新 更多